react-native 调用原生方法

Posted 森森森0w0

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了react-native 调用原生方法相关的知识,希望对你有一定的参考价值。

基于RN0.40。

MyNativeModule.java

package com.www;  //路径名字
 
import android.content.Context;
import android.widget.Toast;
 
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

public class MyNativeModule extends ReactContextBaseJavaModule {
 
  public static final String REACTCLASSNAME = "MyNativeModule";
  private Context mContext;
 
  public MyNativeModule(ReactApplicationContext reactContext) {
    super(reactContext);
    mContext = reactContext;
  }
 
  @Override
  public String getName() {
    return REACTCLASSNAME;
  }
 
  /**
   * 必须添加反射注解不然会报错
   * 这个方法就是ReactNative将要调用的方法,会通过此类名字调用
   * @param msg
   */
  @ReactMethod
  public void callNativeMethod(String msg) {
    Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
  }
}

 

MyReactPackage.java

package com.www;
 
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.javascriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
public class MyReactPackage implements ReactPackage {
 
  @Override
  public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
    List<NativeModule> modules = new ArrayList<>();
    modules.add(new MyNativeModule(reactContext));
    return modules;
  }
 
  @Override
  public List<Class<? extends JavaScriptModule>> createJSModules() {
    return Collections.emptyList();
  }
 
  @Override
  public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
    return Collections.emptyList();
  }
}

 

MainApplication.java

package com.www;

import android.app.Application;
import android.util.Log;

import com.facebook.react.ReactApplication;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;

import java.util.Arrays;
import java.util.List;

public class MainApplication extends Application implements ReactApplication {

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    @Override
    protected boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
          new MyReactPackage()  //只需添加这里
      );
    }
  };

  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);
  }
}

 

index.android.js

import React, {Component} from ‘react‘;
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  NativeModules,
} from ‘react-native‘;
 
class www extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.instructions} onPress={() => this.onClick()}>
          点我
        </Text>
      </View>
    );
  }
 
  /**
   * 调用原生方法
   */
  onClick() {
    NativeModules.MyNativeModule.callNativeMethod(‘成功调用原生方法‘);
  }
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: ‘center‘,
    alignItems: ‘center‘,
    backgroundColor: ‘#F5FCFF‘,
  },
  instructions: {
    textAlign: ‘center‘,
    color: ‘#333333‘,
    marginBottom: 5,
  },
});
 
AppRegistry.registerComponent(‘www‘, () => www);

 

以上是关于react-native 调用原生方法的主要内容,如果未能解决你的问题,请参考以下文章

如何实现React原生倒计时圈

React-Native开发之原生模块封装(Android)升级版

在 Android 上反应原生 Linking.getInitialUrl 总是调用

react-native--基础(方法调用)

react-native--基础(方法调用)

React Native之底层源码分析篇