1.0.4 • Published 1 year ago

react-native-use-native v1.0.4

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

react-native-use-native

A tool to use native code by JavaScript. Only some Android classes are now supported.

Updates

Now lastest version is: 1.0.4 Since version 1.0.3, $app is an ReactApplicationContext class Object, $appnative is a Context class Object. But, you only use string $appnative. Start from version 1.0.4, you can use $appnative.

Version 1.0.3 Updates

Mainly Update: 1. Since version 1.0.3, the non-basic type will return the internal name(such as $3) not string Refrence Type. 2. Support Array and List. 3. Support the Boolean values.

Requirements

react-native == 0.70

The lower version and greater version is not tested.

Installation

npm install react-native-use-native

Usage

The basic is to use useJavaStatic method to call call static methods and use useJava() method to call non-static methods.

Method declaration

useJavaStatic(className:string,methodName:string,params:any[],packageName:string=""):Promise<string>
useJava(objectName:string,methodName:string,params:any[]):Promise<string>

In useJavaStatic, when packageName=="", the className must be the full name includes package name such as android.widget.Toast not Toast. Of Course, you can write Toast in className and android.widget in packageName.

An example about Toast:

import {useJavaStatic,useJava,$app} from 'react-native-use-native';
const toast=await useJavaStatic("android.widget.Toast","makeText",[$app,"HelloToast",0]);
await useJava(toast,"show",[]);

The $app refers to the ReactApplicationContext of now application. You also can directly use useJavaNative. Please see the source code to know more.

use Libraries

Some classes(such as Toast) is packaged to JavaScript/TypeScript Classes, you can directly use these classes.

import {Toast} from 'react-native-use-native';
let toast=await Toast.makeText($app,"HelloToast",Toast.LENGTH_SHORT);
await toast.show();

Create Java Object

You also can create a Java Object by using createJavaObject. You can use getJavaObjectValue to get the value of below's type:int,float,double,Integer,Float,Double and String. Else type will return the string value:Refrence Type. It only can find the object that returned in JS or created by createJavaObject.

Notes

The Java/Android packages you want to use must be imported in Java Code.

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with create-react-native-library