Detailed explanation of native Development of React-native bridge Android,

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.