1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| package com.lqd.androidpractice.rn;
import android.app.Activity; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.provider.Settings; import android.util.Log; import android.view.KeyEvent;
import androidx.annotation.Nullable;
import com.facebook.react.PackageList; import com.facebook.react.ReactInstanceManager; import com.facebook.react.ReactPackage; import com.facebook.react.ReactRootView; import com.facebook.react.common.LifecycleState; import com.facebook.react.devsupport.RedBoxHandler; import com.facebook.react.devsupport.interfaces.StackFrame; import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler; import com.facebook.soloader.SoLoader; import com.lqd.androidpractice.BuildConfig;
import java.util.List;
public class MyReactActivity extends Activity implements DefaultHardwareBackBtnHandler {
private ReactRootView mReactRootView; private ReactInstanceManager mReactInstanceManager;
private final int OVERLAY_PERMISSION_REQ_CODE = 1;
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState);
Log.w("MyReactActivity", "onCreate>>>>>>>>>>>>>>>>>>>>>>>>");
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); } }
SoLoader.init(this, false); mReactRootView = new ReactRootView(this); List<ReactPackage> packages = new PackageList(getApplication()).getPackages();
packages.add(new IndexPackage());
mReactInstanceManager = ReactInstanceManager.builder() .setApplication(getApplication()) .setCurrentActivity(this) .setBundleAssetName("index.android.bundle") .setJSMainModulePath("index") .addPackages(packages) .setUseDeveloperSupport(BuildConfig.DEBUG) .setInitialLifecycleState(LifecycleState.RESUMED) .setRedBoxHandler(new MyRedBoxHandler()) .build();
mReactRootView.startReactApplication(mReactInstanceManager, "MyReactNativeApp", null);
setContentView(mReactRootView); }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode == OVERLAY_PERMISSION_REQ_CODE) { if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if(!Settings.canDrawOverlays(this)) {
} } } mReactInstanceManager.onActivityResult(this, requestCode, resultCode, data); }
@Override public boolean onKeyUp(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_MENU && mReactInstanceManager != null) { mReactInstanceManager.showDevOptionsDialog(); return true; }
return super.onKeyUp(keyCode, event); }
@Override public void invokeDefaultOnBackPressed() { super.onBackPressed(); }
@Override public void onBackPressed() { if (mReactInstanceManager != null) { mReactInstanceManager.onBackPressed(); } else { 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); }
if (mReactRootView != null) { mReactRootView.unmountReactApplication(); } }
}
class MyRedBoxHandler implements RedBoxHandler { @Override public void handleRedbox(@Nullable String title, StackFrame[] stack, ErrorType errorType) {
for(StackFrame stackFrame : stack) { Log.w("MyReactActivity", "=====handleRedbox " + stackFrame.getMethod() + " " + stackFrame.getFileName() + " " + stackFrame.getLine() + " " + stackFrame.getColumn() + " "); } Log.w("MyReactActivity", ">>handleRedbox " + title + "---------------- " + errorType + " "); }
@Override public boolean isReportEnabled() { return true; }
@Override public void reportRedbox(Context context, String title, StackFrame[] stack, String sourceUrl, ReportCompletedListener reportCompletedListener) { Log.w("MyReactActivity", "-----handleRedbox " + title + " sourceUrl:" + sourceUrl + " "); } }
|