Detailed explanation of native Development of React-native bridge Android,
One day in the long history of RN development, I would like to introduce the ups and downs of Android native development. for a little white who does not understand android, it is a little difficult at the beginning, but it is always difficult at the beginning. the language is clear, and the principle is similar.
In the development process, we need to integrate AMAP's navigation function without finding a good wheel. We only need to write native code and then use JS to call native navigation modules.
First, register the module
The meaning of this function is that it can be called with JS only when the class is registered to RN.
public class AnExampleReactPackage 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 NaviActivity(reactContext)); return modules; }}
Modules. add (new NaviActivity (reactContext); add an android native activity module.
This module can define a scheme, and RN can be called directly (@ ReactMethod must be declared on the method)
@ ReactMethod public void showFengMap (String mapID) {Activity currentActivity = getCurrentActivity (); Intent intent = new Intent (currentActivity, Page name. class); currentActivity. startActivity (intent );}
Do I go to other pages? Here, I can also do some other operations, for example, share them directly.
Statement
In the MainApplication of the Android app,
@Override protected List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList( new MainReactPackage(), new AnExampleReactPackage() ); }
Add the package name you just registered
JS calls native code
import { NativeModules } from 'react-native';export default NativeModules.NaviActivity;
The author writes an untils/CommonAndroidUntils. js file here, and directly introduces this js file on the required page.
CommonAndroidUntils.show();
Implement redirection.
Integrated AMAP navigation
It was a little difficult for a Tom to directly integrate AMAP in android studio at the beginning. However, after understanding it, I felt fine. I 'd like to briefly discuss the problems and remind myself to help others.
The. jar audio package directly dragged into cannot be introduced.
The solution is to right-click the. jar package and click the option to introduce. jar. Wait for synchronization.
The rest are minor issues, which are easily solved based on the demo and documentation.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.