react native 怎么调用java的接口 react-native怎么调用android原生

\u7528react native\u600e\u4e48\u8c03\u7528\u540e\u53f0\u63a5\u53e3

\u6211\u7406\u89e3\u7684\u662f\uff0creactjs\u662f\u7ed9\u4e86\u524d\u7aef\u5f00\u53d1\u4eba\u5458\u4e00\u4e2a\u4f7f\u7528js\u5f00\u53d1\u539f\u751f\u5e94\u7528\u7684\u673a\u4f1a\uff0c\u63d0\u4f9b\u4e86\u4e00\u5957\u5404\u4e2a\u5e73\u53f0\u901a\u7528\u7684\u8bed\u6cd5\u7ed3\u6784api\u7b49\u7b49\uff0c\u4f7f\u524d\u7aef\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u4f7f\u7528\u73b0\u6709\u7684\u77e5\u8bc6\u8fdb\u884c\u79fb\u52a8\u5e73\u53f0APP\u7684\u5f00\u53d1\uff0c\u662f\u4f7f\u7528\u76f8\u540c\u7684\u77e5\u8bc6\u8fdb\u884c \u4e0d\u540c\u5e73\u53f0\u7684\u5f00\u53d1\uff0c\u4e5f\u5c31\u662f\u4f60\u8981\u4e3aios\u548candroid\u540c

public class RnTest extends ReactContextBaseJavaModule {
public RnTest(ReactApplicationContext reactContext) {
super(reactContext);
}
// ReactContextBaseJavaModule\u8981\u6c42\u6d3e\u751f\u7c7b\u5b9e\u73b0getName\u65b9\u6cd5\u3002\u8fd9\u4e2a\u51fd\u6570\u7528\u4e8e\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32
// \u8fd9\u4e2a\u5b57\u7b26\u4e32\u7528\u4e8e\u5728JavaScript\u7aef\u6807\u8bb0\u8fd9\u4e2a\u539f\u751f\u6a21\u5757
@Override
public String getName() {
return "ToastByAndroid";
}
// \u83b7\u53d6\u5e94\u7528\u5305\u540d
// \u8981\u5bfc\u51fa\u4e00\u4e2a\u65b9\u6cd5\u7ed9JavaScript\u4f7f\u7528\uff0cJava\u65b9\u6cd5\u9700\u8981\u4f7f\u7528\u6ce8\u89e3@ReactMethod
@ReactMethod
public void getPackageName() {
String name = getReactApplicationContext().getPackageName();
Toast.makeText(getReactApplicationContext(),name,Toast.LENGTH_LONG).show();
}
}

public class RnTest extends ReactContextBaseJavaModule {
public RnTest(ReactApplicationContext reactContext) {
super(reactContext);
}
// ReactContextBaseJavaModule要求派生类实现getName方法。这个函数用于返回一个字符串
// 这个字符串用于在JavaScript端标记这个原生模块
@Override
public String getName() {
return "ToastByAndroid";
}
// 获取应用包名
// 要导出一个方法给JavaScript使用,Java方法需要使用注解@ReactMethod
@ReactMethod
public void getPackageName() {
String name = getReactApplicationContext().getPackageName();
Toast.makeText(getReactApplicationContext(),name,Toast.LENGTH_LONG).show();

扩展阅读:react native webview ... react native ui库 ... react native onedrive ... react框架 ... native speaker ... android studio ... react native flutter ... native官网 ... react native入门教程 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网