RN怎么与native交互的呢?
下面我们通过一个简单的Demo来实现:RN页面调起Native页面,Native页面选择电话本数据,将数据回传给RN展示。
首先是 Native侧
1、MainActivity
- package com.rnandroid01;
-
- import android.content.Intent;
- import android.database.Cursor;
- import android.net.Uri;
- import android.provider.ContactsContract;
-
- import com.facebook.react.ReactActivity;
-
- public class MainActivity extends ReactActivity {
-
-
- @Override
- protected String getMainComponentName() {
- return "RNAndroid01";
- }
-
-
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if(requestCode!=200 || resultCode!=RESULT_OK) return;
- Uri contactData = data.getData();
-
- Cursor cursor = managedQuery(contactData, null, null, null, null);
- cursor.moveToFirst();
- String num = getContactPhone(cursor);
-
-
-
-
- MainApplication.getMyReactPackage().myNativeModule.sendMsgToRn(num);
-
- }
-
-
- private String getContactPhone(Cursor cursor) {
-
- int phoneColumn = cursor
- .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
- int phoneNum = cursor.getInt(phoneColumn);
- String result = "";
- if (phoneNum > 0) {
-
- int idColumn = cursor.getColumnIndex(ContactsContract.Contacts._ID);
- String contactId = cursor.getString(idColumn);
-
- Cursor phone = getContentResolver().query(
- ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
- null,
- ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="
- + contactId, null, null);
- if (phone.moveToFirst()) {
- for (; !phone.isAfterLast(); phone.moveToNext()) {
- int index = phone
- .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
- int typeindex = phone
- .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
- int phone_type = phone.getInt(typeindex);
- String phoneNumber = phone.getString(index);
- result = phoneNumber;
- }
- if (!phone.isClosed()) {
- phone.close();
- }
- }
- }
- return result;
- }
-
- }
2、MainApplication
- package com.rnandroid01;
-
- 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 java.util.Arrays;
- import java.util.List;
-
- public class MainApplication extends Application implements ReactApplication {
-
-
- private static final MyReactPackage myReactPackage=new MyReactPackage();
-
- public static MyReactPackage getMyReactPackage() {
- return myReactPackage;
- }
-
- 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(),
- myReactPackage
- );
- }
- };
-
- @Override
- public ReactNativeHost getReactNativeHost() {
- return mReactNativeHost;
- }
- }
3、MyReactPackage
- package com.rnandroid01;
-
- 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 {
-
- public MyNativeModule myNativeModule;
-
- @Override
- public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
- List<NativeModule> modules=new ArrayList<>();
- myNativeModule=new MyNativeModule(reactContext);
- modules.add(myNativeModule);
- return modules;
- }
-
- @Override
- public List<Class<? extends JavaScriptModule>> createJSModules() {
- return Collections.emptyList();
- }
-
- @Override
- public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
- return Collections.emptyList();
- }
- }
4、MyNativeModule
- package com.rnandroid01;
-
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.provider.ContactsContract;
- import android.widget.Toast;
-
- import com.facebook.react.bridge.ReactApplicationContext;
- import com.facebook.react.bridge.ReactContextBaseJavaModule;
- import com.facebook.react.bridge.ReactMethod;
- import com.facebook.react.modules.core.DeviceEventManagerModule;
-
-
- public class MyNativeModule extends ReactContextBaseJavaModule {
-
- private ReactApplicationContext mContext;
-
- public MyNativeModule(ReactApplicationContext reactContext) {
- super(reactContext);
-
- mContext = reactContext;
- }
-
- @Override
- public String getName() {
-
- return "MyNativeModule";
- }
-
-
-
-
-
- @ReactMethod
- public void rnCallNative(String msg) {
- Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show();
-
-
-
-
- Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
- Bundle bundle = new Bundle();
- mContext.startActivityForResult(intent,200,bundle);
-
-
- }
-
- public void sendMsgToRn(String msg){
-
- mContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit("AndroidToRNMessage",msg);
-
- }
-
- }
在RN侧
-
- import React, { Component } from ‘react‘;
- import {
- AppRegistry,
- StyleSheet,
- Text,
- NativeModules,
- DeviceEventEmitter,
- View
- } from ‘react-native‘;
-
- class RNAndroid01 extends Component {
-
-
- componentWillMount(){
- DeviceEventEmitter.addListener(‘AndroidToRNMessage‘,this.handleAndroidMessage);
- }
-
- componentWillunMount(){
- DeviceEventEmitter.remove(‘AndroidToRNMessage‘,this.handleAndroidMessage);
- }
-
-
- handleAndroidMessage=(msg)=>{
-
- console.log(msg);
- }
-
-
- render() {
- return (
- <View style={styles.container}>
- <Text style={styles.welcome}
- onPress={this.CallAndroid}
- >
- Welcome to React Native!RN与Android的通信
- </Text>
- <Text style={styles.instructions}>
- To get started, edit index.android.js
- </Text>
- <Text style={styles.instructions}>
- Shake or press menu button for dev menu
- </Text>
- </View>
- );
- }
-
-
- CallAndroid=()=>{
- NativeModules.MyNativeModule.rnCallNative(‘rn调用原生模块的方法-成功啦‘);
- }
- }
上面的例子实现了RN与Native端的交互及数据传递。
重点使用了React Native的RCTDeviceEventEmitter,通过消息机制来实现。
好了,RN与native的交互还可以通过Promise和Callback回调方式实现,我们下篇文章再看。
引用原文:http://blog.csdn.net/codetomylaw/article/details/51926421
写博客是为了记住自己容易忘记的东西,另外也是对自己工作的总结,文章可以转载,无需版权。希望尽自己的努力,做到更好,大家一起努力进步!
如果有什么问题,欢迎大家一起探讨,代码如有问题,欢迎各位大神指正!
《React-Native系列》RN与native交互与数据传递
原文:http://www.cnblogs.com/summary-2017/p/7559682.html