React Native與Android交互

React Native與Android交互

Android引用React Native Component

步驟

  1. 新建一個Android專案

  2. 在Android專案根目錄下執行以下指令,提示大部分按enter就好了

    npm init

  3. 繼續執行

    npm install react react-native --save

  4. 這時Android的目錄會是以下的樣子

     -YourProjectName
     |-.gradle
     |-.idea
     |-app
     |-build
     |-gradle
     |-node_modules
     |-.gitignore
     |-YourProjectName.iml
     |-build.gradle
     |-gradle.properties
     |-gradlew
     |-gradlew.bat
     |-local.properties
     |-npm-debug.log
     |-package.json
     |-settings.gradle
     |-yarn.lock
    
  5. package.json的內容應該為下,react和react-native版本會隨著時間推移改變

     {
       "name": "YourProjectName",
       "version": "1.0.0",
       "description": "",
       "main": "index.js",
       "scripts": {
         "test": "echo \"Error: no test specified\" && exit 1"
       },
       "author": "",
       "license": "ISC",
       "dependencies": {
         "react": "^16.2.0",
         "react-native": "^0.52.2"
       }
     }
    
  6. 接著開啟Android專案的root gradle,將剛剛npm下載的react native
    Android library放到repositories中

     allprojects {
       repositories {
           jcenter()
           maven { url 'https://maven.google.com' }
           // 將node_modules中Android相關的lib放到maven引用中
           maven {
               // All of React Native (JS, Android binaries) is installed from npm
               url "$rootDir/node_modules/react-native/android"
           }
       }
     }
    
  7. 接著到app的gradle中,將react native相關的lib引入

     dependencies {
         ... ...
         compile "com.facebook.react:react-native:+" // From node_modules.
     }
    
  8. 配置完成,打開MainActivity,在onCraete中

      //檢查權限:讓使用者打開懸浮視窗權限以便開發中的紅屏錯誤能正確顯示
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
             if (!Settings.canDrawOverlays(this)) {
                 Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                         Uri.parse("package:" + getPackageName()));
                 startActivityForResult(intent, 154);
             }
         }
    
  9. Android端的作業做得差不多了,現在來寫一下react native的部分吧,在Android專案根目錄新增一個目錄js,在裡面新增一個index.js,這個就是等等Activity要用來顯示的react native component

     import React from 'react';
     import {
       AppRegistry,
       StyleSheet,
       Text,
       View
     } from 'react-native';
     
     class HelloWorld extends React.Component {
       render() {
         return (
           <View style={styles.container}>
             <Text style={styles.text}>I'm React Native Text</Text>
           </View>
         )
       }
     }
     var styles = StyleSheet.create({
       container: {
         flex: 1,
         justifyContent: 'center',
       },
       text: {
         fontSize: 20,
         textAlign: 'center',
         margin: 10,
       },
     });
     
     AppRegistry.registerComponent('MyReactNativeApp', () => HelloWorld);
    
  10. 接下來在Activity中

     class MainActivity extends AppCompatActivity {
    
         private ReactRootView mReactRootView;
         private ReactInstanceManager mReactInstanceManager;
    
         @Override
         protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
    
             mReactRootView = new ReactRootView(this);
             mReactInstanceManager = ReactInstanceManager.builder()
                     .setApplication(getApplication())
                     // 在asset文件夾中,打包過的react native js文件名稱
                     .setBundleAssetName("index.android.bundle")
                     .addPackage(new MainReactPackage())
                     .setUseDeveloperSupport(BuildConfig.DEBUG)
                     .setInitialLifecycleState(LifecycleState.RESUMED)
                     .build();
    
             // 第二個參數"MyReactNativeApp"為React Native中的AppRegistry.registerComponent的第一個參數
             mReactRootView.startReactApplication(mReactInstanceManager, "MyReactNativeApp", null);
    
             setContentView(mReactRootView);
         }
     }
    
  11. 將剛剛寫好的react native component打包成bundle,給ReactInstanceManager使用,bundle會放在Android的asset下

    react-native bundle --platform {{平台}} --entry-file {{入口文件,一般命名index.js}} --bundle-output {{打包好的bundle存放路徑}} --assets-dest {{react native引用的資源文件置放路徑}}
    
    react-native bundle --platform android --entry-file ./app/js/index.js --bundle-output app/src/main/assets/index.android.bundle --assets-dest app/src/main/res/
    
  12. 恭喜!執行App,看到React Native的介面

Android 呼叫 React Native Component

  1. 新增一個implement ReactContextBaseJavaModule的class,這個class是最終與react native通信的類

     class ToastAndroidModule(private val reactContext: ReactApplicationContext) :
         ReactContextBaseJavaModule(reactContext) {
    
         /**
          * react native call android時的模組名稱
          */
         override fun getName(): String {
             return "ToastAndroidModule"
         }
     
         /**
          * react native call(->) android
          */
         @ReactMethod // 此註解代表要expose給react native的方法
         fun HandleMessage(message: String) {
             Toast.makeText(reactContext, message, Toast.LENGTH_LONG).show()
         }
     
         /**
          * android call(->) react native
          */
         fun sendMessage(params: String) {
             reactContext
                     .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
                     .emit("mEventName", params)
         }
     }
    
  2. 新增一個implement ReactPackage的Package,此類底下會有許多NativeModule(就是第一步驟寫的),必須將此類放入react native API,才能進行溝通

     class AndroidWidgetPackage : ReactPackage {
    
         private var nativeModules: MutableList<NativeModule>? = null
     
         override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
             // 在這裡將需要溝通原生模組放入list並回傳,這樣就等於將原生註冊到react native
             nativeModules = ArrayList()
             nativeModules!!.add(0, ToastAndroidModule(reactContext))
             return nativeModules as ArrayList<NativeModule>
         }
     
         override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
             return Collections.emptyList()
         }
     
         /**
          * 方便取得AndroidModule的方法
          */
         fun getModule(index: Int): NativeModule? {
             return if (nativeModules == null) null else nativeModules!![index]
         }
     
         fun getToastModule(): ToastAndroidModule {
             return getModule(0) as ToastAndroidModule
         }
     }
    
  3. 撰寫React Native的Component,此Component功能很簡單,點擊按鈕呼叫第一部撰寫的ToastAndroidModule中的HandleMessage,然後就會看到Toast出現

      import React from 'react';
      import {
        AppRegistry,
        StyleSheet,
        Text,
        View,
        TouchableOpacity,
        Dimensions,
        NativeModules,
        ToastAndroid,
        DeviceEventEmitter
      } from 'react-native';
     
     export default class Communication3 extends React.Component {
     
           constructor(){
             super();
             this.state = {
                 info : "我是React Native寫的內容"
             }
           }
     
         componentWillMount(){
           DeviceEventEmitter.addListener('mEventName',
                                this.rnMethod.bind(this));
         }
     
         rnMethod(params){
           this.setState({info:params});
         }
     
        render() {
          return (
            <TouchableOpacity style={styles.container}>
               <View style={{width:Dimensions.get('window').width,height:50,margin:10,
                   backgroundColor:'#dfd',alignItems:'center',justifyContent:'center'}}>
                     <Text style={styles.hello}>{this.state.info}</Text>
               </View>
            </TouchableOpacity>
          )
        }
      }
      var styles = StyleSheet.create({
        container: {
          flex: 1,
          justifyContent: 'center',
        }
     });
    
  4. 將剛剛寫好的react native component打包成bundle,給ReactInstanceManager使用,bundle會放在Android的asset下

     react-native bundle --platform {{平台}} --entry-file {{入口文件,一般命名index.js}} --bundle-output {{打包好的bundle存放路徑}} --assets-dest {{react native引用的資源文件置放路徑}}
     
     react-native bundle --platform android --entry-file ./app/js/index.js --bundle-output app/src/main/assets/index.android.bundle --assets-dest app/src/main/res/
    
  5. 接下來撰寫Activity的View

     <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/root_view"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical">
         <Button
             android:id="@+id/native_btn"
             android:layout_width="match_parent"
             android:layout_height="40dp"
             android:layout_margin="10dp"
             android:background="#ddf"
             android:text="我是原生按鈕點擊我調用React Native方法" />
             
         <com.facebook.react.ReactRootView
             android:layout_weight="1"
             android:id="@+id/react_root_view1"
             android:layout_width="match_parent"
             android:layout_height="wrap_content" />
             
     </LinearLayout>
    
  6. 最後就是要將ReactPackage註冊到React Native API中了

     class ReactCommunicationActivity : AppCompatActivity() {
         
         private var mReactRootView: ReactRootView? = null
         private var mReactInstanceManager: ReactInstanceManager? = null
         private var reactPackage: AndroidWidgetPackage? = null
         private var mClickTime = 0
     
         override fun onCreate(savedInstanceState: Bundle?) {
             super.onCreate(savedInstanceState)
             setContentView(R.layout.activity_react_comunucatuin)
             
             mReactRootView = ReactRootView(this)
             reactPackage = AndroidWidgetPackage()
             mReactInstanceManager = ReactInstanceManager.builder()
                     .setApplication(application)
                     .setBundleAssetName("index.android.bundle")
                     .setJSMainModulePath("index")
                     .addPackage(MainReactPackage())
                     .addPackage(reactPackage)   //加入AndroidModule
                     .setUseDeveloperSupport(BuildConfig.DEBUG)
                     .setInitialLifecycleState(LifecycleState.RESUMED)
                     .build()
     
             // 注意這裡的MyReactNativeApp必須對應“index.android.js”中的
             // “AppRegistry.registerComponent()”的第一個參數
             react_root_view1.startReactApplication(mReactInstanceManager, "Communication2", null)
     
     
             //添加本地按鈕的點擊事件
             native_btn.setOnClickListener {
                 reactPackage!!.getToastModule().sendMessage("這是一條Android發送給React的消息${mClickTime++}")
             }
         }
     }
    
  7. 點擊按鈕,成功調用原生的Toast方法

React Native Component 呼叫 Activity

  1. 在上一部的基礎上,在View加上

     <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/root_view"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="vertical">
    
     <Button
         android:id="@+id/native_btn"
         android:layout_width="match_parent"
         android:layout_height="40dp"
         android:layout_margin="10dp"
         android:background="#ddf"
         android:text="我是原生按鈕點擊我調用React Native方法" />
    
    
     <com.facebook.react.ReactRootView
         android:layout_weight="1"
         android:id="@+id/react_root_view1"
         android:layout_width="match_parent"
         android:layout_height="wrap_content" />
    
     <!--新增一個react view-->
     <com.facebook.react.ReactRootView
         android:layout_weight="1"
         android:id="@+id/react_root_view2"
         android:layout_width="match_parent"
         android:layout_height="wrap_content" />
    
     </LinearLayout>
    
  2. 新增一個Component,此Componenet目的為顯示Android端點擊後,反應點擊

     import React from 'react';
     import {
        AppRegistry,
        StyleSheet,
        Text,
        View,
        TouchableOpacity,
        Dimensions,
        NativeModules,
        ToastAndroid
     } from 'react-native';
     
     export default class Communication extends React.Component {
     
       onPress = ()=> {
           // 這樣調用原生端方法,show出吐司
           NativeModules.ToastAndroidModule
           .HandleMessage("React Native 呼叫Native来吐司!!");
       }
     
        render() {
          return (
            <TouchableOpacity style={styles.container} 
            onPress = {this.onPress.bind(this)}>
               <View style={{
               width:Dimensions.get('window').width,
               height:50,
               backgroundColor:'#dfd',
               alignItems:'center',
               justifyContent:'center'
               }}>
                 <Text style={styles.text}>
                 這是一個React Native按鈕,點擊調用原生Toast方法
                 </Text>
               </View>
            </TouchableOpacity>
          )
        }
      }
      var styles = StyleSheet.create({
        container: {
          flex: 1,
          justifyContent: 'center',
        },
        text:{
            fontSize: 20
        }
     });
    
  3. 打包js

     # react-native bundle --platform {{平台}} --entry-file {{入口文件,一般命名index.js}} --bundle-output {{打包好的bundle存放路徑}} --assets-dest {{react native引用的資源文件置放路徑}}
     
     react-native bundle --platform android --entry-file ./app/js/index.js --bundle-output app/src/main/assets/index.android.bundle --assets-dest app/src/main/res/
    
  4. 在Activity中,啟動該React Native Component的生命週期

     class ReactCommunicationActivity : AppCompatActivity() {    
         private var mReactRootView: ReactRootView? = null
         private var mReactInstanceManager: ReactInstanceManager? = null
         private var reactPackage: AndroidWidgetPackage? = null
         private var mClickTime = 0
     
         override fun onCreate(savedInstanceState: Bundle?) {
             super.onCreate(savedInstanceState)
             setContentView(R.layout.activity_react_comunucatuin)
     
             mReactRootView = ReactRootView(this)
             reactPackage = AndroidWidgetPackage()
             mReactInstanceManager = ReactInstanceManager.builder()
                     .setApplication(application)
                     .setBundleAssetName("index.android.bundle")
                     .setJSMainModulePath("index")
                     .addPackage(MainReactPackage())
                     .addPackage(reactPackage)   //加入AndroidModule
                     .setUseDeveloperSupport(BuildConfig.DEBUG)
                     .setInitialLifecycleState(LifecycleState.RESUMED)
                     .build()
     
             // 注意這裡的MyReactNativeApp必須對應“index.android.js”中的
             // “AppRegistry.registerComponent()”的第一個參數
     //        mReactRootView!!.startReactApplication(mReactInstanceManager, "Communication3", null)
             react_root_view1.startReactApplication(mReactInstanceManager, "Communication2", null)
             react_root_view2.startReactApplication(mReactInstanceManager, "Communication3", null)
     
     
             //添加本地按鈕的點擊事件
             native_btn.setOnClickListener {
                 reactPackage!!.getToastModule().sendMessage("這是一條Android發送給React的消息${mClickTime++}")
             }
         }
     }
    
  5. 測試,完工!

將現有的Android專案整React Native

// TODO

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,761评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,953评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,998评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,248评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,130评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,145评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,550评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,236评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,510评论 1 291
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,601评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,376评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,247评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,613评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,911评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,191评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,532评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,739评论 2 335

推荐阅读更多精彩内容