该篇文章实现方法其实和之前的第八篇-React Native实现调用android原生java方法并实现广播的发送和接受,方法类似,只不过调用原生方法里的动作不同,一个是启动servicer,一个是启动activity。所以本篇不多讲直接先看效果:
一、用AS打开项目目录的android项目,并添加一个activity,该activity中未添加任何逻辑操作:
package com.myreduxproject;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class UserInfoActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_info);
}
}
二、添加MyIntentModule类,并继承ReactContextBaseJavaModule实现其方法和构造函数。在该类中添加方法,注意:方法头要加@ReactMethod
package com.myreduxproject;
import android.app.Activity;
import android.content.Intent;
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
/** * Created by Administrator on 2017/5/22. */
public class MyIntentModule extends ReactContextBaseJavaModule {
public MyIntentModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return "IntentMoudle";
}
@ReactMethod
public void startActivityFromJS(String name,String params){
try{
Activity currentActivity = getCurrentActivity();
if(null!=currentActivity){
Class toActivity = Class.forName(name);
Intent intent = new Intent(currentActivity,toActivity);
intent.putExtra("params",params);
currentActivity.startActivity(intent);
}
}catch(Exception e){
throw new JSApplicationIllegalArgumentException(
"不能打开Activity : "+e.getMessage());
}
}
//注意:记住getName方法中的命名名称,JS中调用需要
//注意:startActivityFromJS方法添加RN注解(@ReactMethod),否则该方法将不被添加到RN中
}
三、添加MyReactPackage类,实现ReactPackage接口里的方法,在重写方法createNativeModules里注册上一步添加的模块:
package com.myreduxproject;
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.Arrays;
import java.util.Collections;
import java.util.List;
/** * Created by Administrator on 2017/5/22. */
public class MyReactPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(new MyIntentModule(reactContext));
}
@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
四、接着在MainApplication中的getPackages方法中注册到ReactPackage中:
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),**new MyReactPackage(),**
new BaiduMapPackage(getApplicationContext())
);
}
五、接下来的工作便是RN的代码,首先引入NativeModules
import {NativeModules} from 'react-native';
然后通过NativeModules调用我们创建的类中的方法:
NativeModules
.IntentMoudle
.startActivityFromJS("com.myreduxproject.UserInfoActivity",null);