编写自定义插件需要继承ReactContextBaseJavaModule和实现ReactPackage接口, 具体实现步骤如下:
1. 继承ReactContextBaseJavaModule接口
public class IntentModule extends ReactContextBaseJavaModule
2. 重写 getName方法,暴露给JS端调用名
@Override
public String getName() {
return "IntentModule";
}
3. 给暴露给JS的方法添加 @ReactMethod 注解,且方法的返回值只能是void
@ReactMethod
public void backActivity(int count) {
if (count > 0) {
try {
Activity currentActivity = getCurrentActivity();
currentActivity.finish();
} catch (Exception e) {
e.printStackTrace();
}
}
}
4.实现ReactPackage接口
public class IntentPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(
new IntentModule(reactContext)
);
}
@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
5.在Application中注册IntentPackage
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new IntentPackage()
);
}
6.JavaScript调用IntentModule的backActivity方法
NativeModules.IntentModule.backActivity();