Home > Article > Web Front-end > How to implement React-native bridging Android and what are the specific steps?
This article mainly introduces the detailed explanation of React-native bridging Android native development. Now I share it with you and give you a reference.
In the long journey of developing RN, sooner or later one will have to come into contact with native Android development. The author will introduce the ups and downs. For a novice who does not understand Android, it is a bit difficult at the beginning. But everything is difficult at the beginning. The language is clear, and the principle is the same.
During the development process, we need to integrate the navigation function of Amap, but we have not found a good one. For the wheel, just write the native code, and then use JS to call the native navigation module.
First register the module
The meaning is to register the class to RN , you can use JS to call
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; } }
where modules.add(new NaviActivity(reactContext)); means adding an Android native activity module
This module can define the solution, and RN can directly Calling (@ReactMethod must be declared on the method)
@ReactMethod public void showFengMap(String mapID){ Activity currentActivity = getCurrentActivity(); Intent intent = new Intent(currentActivity, 页面名.class); currentActivity.startActivity(intent); }
The author is jumping to other pages. You can also do some other operations here. For example, go directly to share
Statement
In the MainApplication in the Android app,
@Override protected List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList( new MainReactPackage(), new AnExampleReactPackage() ); }
Add the just registered package name
JS calls the native code
import { NativeModules } from 'react-native'; export default NativeModules.NaviActivity;
The author wrote a untils/CommonAndroidUntils.js here, and directly introduced this js file into the page that needs to be used
CommonAndroidUntils.show();
to achieve the jump.
integrationAmap Navigation
For a novice to directly integrate the Amap map in android studio, it is still a little difficult at first. But after understanding it, I feel okay. Let’s simplify the problems encountered, Remind yourself to help others
The .jar voice package dragged directly cannot be imported
The solution is to right-click the .jar package and click the option to import .jar. Just wait for synchronization.
The rest are minor problems that can be easily solved based on the demo and documentation.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future. helpful.
Related articles:
How to use babel installation and configuration tutorial
How to configure the babel configuration file in vue-cli
Use node.js to implement Douyin’s automatic red envelope grabbing function
The above is the detailed content of How to implement React-native bridging Android and what are the specific steps?. For more information, please follow other related articles on the PHP Chinese website!