Home > Web Front-end > JS Tutorial > How to implement React-native bridging Android and what are the specific steps?

How to implement React-native bridging Android and what are the specific steps?

亚连
Release: 2018-06-11 16:29:46
Original
2293 people have browsed it

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;
  }
}
Copy after login

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);
  }
Copy after login

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()
   );
  }
Copy after login

Add the just registered package name

JS calls the native code

import { NativeModules } from &#39;react-native&#39;;

export default NativeModules.NaviActivity;
Copy after login

The author wrote a untils/CommonAndroidUntils.js here, and directly introduced this js file into the page that needs to be used

CommonAndroidUntils.show();
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template