在使用React Native开发过程中,或多或少需要涉及到与原生交互,例如获取App版本号,这里以Android为例。
我们首先需要在Android/app/src/main/java/com文件夹下或者你自己的包名下新建一个reactnative文件夹,并在该文件夹下创建RNBridgeManager.java文件和RNReactPackage.java文件,文件名字可以自定义,也可以起别的文件名,建议见名知意,具体代码如下:
RNBridgeManager.java 中的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
| package com.reactnative;
import android.content.pm.PackageInfo; import android.content.pm.PackageManager;
import com.facebook.react.bridge.Callback; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; import com.facebook.react.uimanager.IllegalViewOperationException;
public class RNBridgeManager extends ReactContextBaseJavaModule {
public RNBridgeManager(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override public String getName() { return "BridgeManager"; }
@ReactMethod public void getAppVersion(Callback successCallback) { try { PackageInfo info = getPackageInfo(); if (info != null) { successCallback.invoke(info.versionName); } else { successCallback.invoke(""); } } catch (IllegalViewOperationException e) {
} }
private PackageInfo getPackageInfo() { PackageManager manager = getReactApplicationContext().getPackageManager(); PackageInfo info = null; try { info = manager.getPackageInfo(getReactApplicationContext().getPackageName(), 0); return info; } catch (Exception e) { e.printStackTrace(); } finally {
return info; } } }
|
RNReactPackage.java 中的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| package com.reactnative;
import com.facebook.react.ReactPackage; import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.uimanager.ViewManager;
import java.util.ArrayList; import java.util.Collections; import java.util.List;
public class RNReactPackage implements ReactPackage {
@Override public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) { return Collections.emptyList(); }
@Override public List<NativeModule> createNativeModules( ReactApplicationContext reactContext) { List<NativeModule> modules = new ArrayList<>();
modules.add(new RNBridgeManager(reactContext));
return modules; }
}
|
然后在MainApplication.java中添加刚注册过的包名
1 2 3 4 5 6 7 8
| @Override protected List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList( new MainReactPackage(), …… new RNReactPackage() ); }
|
最后,在RN中调用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| import React, { Component } from 'react'; import { NativeModules } from 'react-native';
export default class Me extends Component { constructor(props) { super(props);
this.state = { version: '', } this.getVerSion(); }
getVerSion(){ NativeModules.BridgeManager.getAppVersion((event) =>{ this.setState({ version:event }) }); }
|
若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏