React-Native Android - 从意图中获取变量

Nur*_*Bar 8 android android-intent react-native

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

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

   Intent intent = new Intent(this, MainActivity.class);
   Intent.putExtra("alarm",true);
   intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

谢谢!

小智 7

试试这个以反应原生应用程序获取Intent params.

在我的原生应用程序中,我使用此代码:

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

在react-native项目中,我的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());
  }
}
Run Code Online (Sandbox Code Playgroud)

在我的第一个容器中,我得到了this.props中的参数

export default class App extends Component {

    render() {
        console.log('App props', this.props);

        //...
    }
}
Run Code Online (Sandbox Code Playgroud)

我在这里找到的完整示例:http: //cmichel.io/how-to-set-initial-props-in-react-native/