Home  >  Article  >  Web Front-end  >  Detailed explanation of Android native development with React-native

Detailed explanation of Android native development with React-native

小云云
小云云Original
2018-01-17 17:03:131528browse

This article mainly introduces the detailed explanation of React-native bridging Android native development. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor to take a look, I hope it can help everyone.

In the long journey of developing RN, sooner or later one will have to come into contact with the native development of Android. The author will introduce the ups and downs. For a novice who does not understand Android, it is just starting It’s a bit difficult, but everything is difficult at the beginning. The language is clear, and the principles are the same.

During the development process, the navigation function of Amap must be integrated. If you haven't found a good wheel, just write native code, and then use JS to call the native navigation module.

First register the module

The meaning is to combine the classes with Only after registering in RN can you 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 a solution, and RN can call it directly (@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);
  }

Does the author jump to other Page, you can also do some other operations here. For example, directly share the

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 just registered

JS calls the native code

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

export default NativeModules.NaviActivity;

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

CommonAndroidUntils.show();

to achieve jump.

Integration Amap Navigation

For a novice to directly integrate the Amap map in android studio, it is a bit difficult at first. But after understanding it, I feel better. Let’s briefly explain the problems encountered and remind myself , Help others

The .jar voice package dragged directly cannot be imported

The solution is to right-click the .jar package, click on the option to import .jar, and wait for synchronization That’s it.

The rest are minor problems that can be easily solved based on the demo and documentation.


Related recommendations:


React-Native global multi-language switching tool library react-native-i18n detailed explanation

Detailed explanation of props usage in React-Native

Introduction to Image control in React-native

The above is the detailed content of Detailed explanation of Android native development with React-native. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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