React-Native Android-从Intent获取变量 [英] React-Native Android - Get the variables from intent

查看:468
本文介绍了React-Native Android-从Intent获取变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用意图来启动我的React-Native应用程序,并且我试图找出如何获取在我的意图本机代码中放入意图的变量.这可能是在react-native内实现的,还是我必须编写一些Java代码来获取它?

I'm using intent to start my React-Native app, and I'm trying to find out how to get the variables I put on my intent in the react native code. Is this possible from within react-native or do I have to write some java code to get it?

我用来启动应用程序的代码:

the code I use to start the app :

   Intent intent = new Intent(this, MainActivity.class);
   Intent.putExtra("alarm",true);
   intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   startActivity(intent);

谢谢!

推荐答案

尝试通过此方法在react-native应用程序中获取Intent参数.

Try this to get Intent params at react-native app.

在我的本机应用程序中,我使用以下代码:

In my native App, I use this code:

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.my.react.app.package");
launchIntent.putExtra("test", "12331");
startActivity(launchIntent);

在react-native项目中,我的MainActivity.java

In react-native project, my 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中获得了参数

In my first Container I get the param in 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/

The complete example I found here: http://cmichel.io/how-to-set-initial-props-in-react-native/

这篇关于React-Native Android-从Intent获取变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆