React-Native Android - 从意图获取变量
问题描述:
我正在使用意图启动我的React-Native应用程序,并试图了解如何获取我在意图反应本机代码中放置的变量。这可能来自react-native内部,还是我必须编写一些java代码才能得到它?React-Native Android - 从意图获取变量
我用来启动应用程序的代码:
Intent intent = new Intent(this, MainActivity.class);
Intent.putExtra("alarm",true);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
谢谢!
答
试试这个以获取意图参数在react-native应用程序。
在我的本机应用程序,我用这个代码:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.my.react.app.package");
launchIntent.putExtra("test", "12331");
startActivity(launchIntent);
在反应本地的项目,我MainActivity.java
public class MainActivity extends ReactActivity {
@Override
protected String getMainComponentName() {
return "FV";
}
public static class TestActivityDelegate extends ReactActivityDelegate {
private static final String TEST = "test";
private Bundle mInitialProps = null;
private final
@Nullable
Activity mActivity;
public TestActivityDelegate(Activity activity, String mainComponentName) {
super(activity, mainComponentName);
this.mActivity = activity;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
Bundle bundle = mActivity.getIntent().getExtras();
if (bundle != null && bundle.containsKey(TEST)) {
mInitialProps = new Bundle();
mInitialProps.putString(TEST, bundle.getString(TEST));
}
super.onCreate(savedInstanceState);
}
@Override
protected Bundle getLaunchOptions() {
return mInitialProps;
}
}
@Override
protected ReactActivityDelegate createReactActivityDelegate() {
return new TestActivityDelegate(this, getMainComponentName());
}
}
在我的第一个集装箱我得到this.props帕拉姆
export default class App extends Component {
render() {
console.log('App props', this.props);
//...
}
}
完整的例子我发现这里: http://cmichel.io/how-to-set-initial-props-in-react-native/
答
您可以通过初始道具成捆的第三个参数的startReactApplication
方法,像这样:
Bundle initialProps = new Bundle();
initialProps.putString("alarm", true);
mReactRootView.startReactApplication(mReactInstanceManager, "HelloWorld", initialProps);
看到这个答案的详细信息:https://*.com/a/34226172/293280
什么是不以'getIntent工作“第二项活动? –
第二项活动是反应原生的。所以我的问题是我如何获得反应原生getIntent。 –