1.我们有很多项目,想在现有的项目中集成RN,其实是比较简单的,第一步创建Activity:
package com.capmanage.activity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.KeyEvent;
import com.alibaba.fastjson.JSONObject;
import com.capmanage.BuildConfig;
import com.capmanage.views.AppNativePackage;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactRootView;
import com.facebook.react.common.LifecycleState;
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
import com.facebook.react.shell.MainReactPackage;
import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
/**
* Created by chan on 10/01/2018.
*/
public class NewReactViewActivity extends BaseActivity implements DefaultHardwareBackBtnHandler {
public static int OVERLAY_PERMISSION_REQ_CODE = 1234;
private ReactRootView mReactRootView;
private ReactInstanceManager mReactInstanceManager;
static public NewReactViewActivity instance;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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, OVERLAY_PERMISSION_REQ_CODE);
}
}
Intent intent = getIntent();
String paramsString = intent.getStringExtra("params");
instance = this;
mReactRootView = new ReactRootView(this);
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index.android")
.addPackage(new MainReactPackage())
.addPackage(new AppNativePackage())
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
Bundle bundle = new Bundle();
bundle.putString("lanuchOptions",paramsString);
mReactRootView.startReactApplication(mReactInstanceManager, "CapManage", bundle);
setContentView(mReactRootView);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
// SYSTEM_ALERT_WINDOW permission not granted...
}
}
}
}
@Override
public void invokeDefaultOnBackPressed() {
super.onBackPressed();
}
@Override
protected void onPause() {
super.onPause();
if (mReactInstanceManager != null) {
mReactInstanceManager.onHostPause(this);
}
}
@Override
protected void onResume() {
super.onResume();
if (mReactInstanceManager != null) {
mReactInstanceManager.onHostResume(this, this);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mReactInstanceManager != null) {
mReactInstanceManager.onHostDestroy(this);
}
}
@Override
public void onBackPressed() {
if (mReactInstanceManager != null) {
mReactInstanceManager.onBackPressed();
} else {
super.onBackPressed();
}
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU && mReactInstanceManager != null) {
mReactInstanceManager.showDevOptionsDialog();
return true;
}
return super.onKeyUp(keyCode, event);
}
}
2.修改MainApplication,如下:
package com.capmanage;
import android.app.Application;
import com.capmanage.host.CoreReacNativeHost;
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 static MainApplication instance;
private final ReactNativeHost mReactNativeHost = new CoreReacNativeHost(this);
@Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
@Override
public void onCreate() {
super.onCreate();
instance = this;
SoLoader.init(this, /* native exopackage */ false);
}
public static MainApplication getInstance() {
return instance;
}
public void initReactInstance(){
ReactInstanceManager manage = this.getReactNativeHost().getReactInstanceManager();
if(!manage.hasStartedCreatingInitialContext()){
manage.createReactContextInBackground();
}
}
public static ReactInstanceManager getReactInstanceManager(){
return MainApplication.getInstance().getReactNativeHost().getReactInstanceManager();
}
}
3.封装ReactNativeHost,如下:
package com.capmanage.host;
import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import com.capmanage.BuildConfig;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nullable;
//import com.kuaibao.skuaidi.react.views.msgtemplate.MsgTemplatePackage;
/**
* Created by chan on 9/23/17.
*/
public class CoreReacNativeHost extends ReactNativeHost {
@Override
public boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected String getJSMainModuleName() {
return "index.android";
}
public CoreReacNativeHost(Application application) {
super(application);
}
public boolean isUseDeveloperSupport(){
return this.getUseDeveloperSupport();
}
@Override
protected List<ReactPackage> getPackages() {
ArrayList<ReactPackage> packages=new ArrayList<ReactPackage>();
packages.add(new MainReactPackage());
return packages;
}
public String getAppMetaData(Context context, String key) {
if (context == null || TextUtils.isEmpty(key)) {
return null;
}
String channelNumber = null;
try {
PackageManager packageManager = context.getPackageManager();
if (packageManager != null) {
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
if (applicationInfo != null) {
if (applicationInfo.metaData != null) {
channelNumber = applicationInfo.metaData.getString(key);
}
}
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return channelNumber;
}
}
4.MainActivity跳转rn页面,如下:
package com.capmanage;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import com.alibaba.fastjson.JSONObject;
import com.capmanage.activity.BaseActivity;
import com.capmanage.activity.NewReactViewActivity;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends BaseActivity {
public static int OVERLAY_PERMISSION_REQ_CODE = 1234;
/**
* Returns the name of the main component registered from JavaScript.
* This is used to schedule rendering of the component.
*/
// @Override
// protected String getMainComponentName() {
// return "CapManage";
// }
//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt_toRN = (Button) this.findViewById(R.id.bt_toRN);
Button btn = (Button)this.findViewById(R.id.bt_left);
bt_toRN.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
HashMap<String, Object> params = new HashMap<>();
Intent intent = new Intent(MainActivity.this, NewReactViewActivity.class);
Map initParams = new HashMap();
initParams.put("page", "Test");
Map userInfo = new HashMap();
userInfo.put("user_id", "123");
Map rootParams = new HashMap();
rootParams.put("initParams",initParams);
rootParams.put("userInfo",userInfo);
intent.putExtra("params", JSONObject.toJSONString(rootParams));
startActivity(intent);
}
});
btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
showPopupWindow();
}
});
MainApplication.getInstance().initReactInstance();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(this)) {
// SYSTEM_ALERT_WINDOW permission not granted...
}
}
}
}
}
5.baseActivity文件如下:
package com.capmanage.activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import com.capmanage.R;
public class BaseActivity extends AppCompatActivity {
PopupWindow popupWindow;
Button rlTest ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//在setContentView之前添加,未添加的话home键监听无效,设置窗体属性
this.getWindow().setFlags(0x80000000, 0x80000000);
setContentView(R.layout.activity_main);
rlTest = (Button)this.findViewById(R.id.bt_left);;
//创建广播
//InnerRecevier innerReceiver = new InnerRecevier();
//动态注册广播
//IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
//启动广播
//registerReceiver(innerReceiver, intentFilter);
//外部网页
// init();
//pop\
}
public void showPopupWindow(){
getPopupWindow();
popupWindow.showAtLocation(rlTest, Gravity.LEFT,0,0);
}
/*创建PopupWindow*/
protected void initPopupWindow(){
//获取自定义布局文件activity_pop_left.xml 布局文件
final View popipWindow_view = getLayoutInflater().inflate(R.layout.activity_pop_left,null,false);
//创建Popupwindow 实例,200,LayoutParams.MATCH_PARENT 分别是宽高
popupWindow = new PopupWindow(popipWindow_view,600, ViewGroup.LayoutParams.MATCH_PARENT,true);
//设置动画效果
popupWindow.setAnimationStyle(R.style.AnimationFade);
//点击其他地方消失
popipWindow_view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (popipWindow_view != null && popipWindow_view.isShown()) {
popupWindow.dismiss();
popupWindow = null;
}
return false;
}
});
popupWindow.setBackgroundDrawable(new ColorDrawable(0));
// Button button1 = (Button) popipWindow_view.findViewById(R.id.button1);
// button1.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// Toast.makeText(getApplicationContext(),"全屏显示", Toast.LENGTH_SHORT).show();
// }
// });
}
/*获取PopipWinsow实例*/
private void getPopupWindow(){
if (null!=popupWindow){
popupWindow.dismiss();
return;
}else {
initPopupWindow();
}
}
}
6.工程下载地址