美文网首页程序员
打开其他app的Activity界面并获取返回值

打开其他app的Activity界面并获取返回值

作者: _琳哥 | 来源:发表于2017-10-30 13:00 被阅读209次

应用场景 : 应用PtDemo唤起PtApp的登录界面LoginActivity并获取返回值

PtApp部分

1. 在 AndroidManifest.xml 中配置 LoginActivity
<activity
        android:name=".me.LoginActivity"
        android:screenOrientation="portrait">
    <intent-filter>
        <action android:name="wyx.subplatform"/> 
        <data android:scheme="subplatform"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
    </intent-filter>
</activity>
  • action 配置Intent的启动action, 为自定义的值
  • data的scheme用于配置协议 , 自定义的值 . 此文配置的为 subplatform://
2. 在 LoginActivity 接收Intent方式一样用getIntent()就好 , 下面为设置页面的返回值
Intent it = new Intent();
it.putExtra("lin", "intent value result ok");
//Bundle bundle = new Bundle();
//bundle.putString("flynn", "bundle value result ok");   这里用bundle好像不能被接收到
setResult(Activity.RESULT_OK, it);
finish();
  • 只需要设置 setResult(Activity.RESULT_OK, it) 就能返回了

PtDemo部分

1. 根据PtApp里面对LoginActivity的配置, 以如下方式打开
Intent it = new Intent("wyx.subplatform", Uri.parse("subplatform://"));
it.putExtra("platformId", "3222");
startActivityForResult(it, 100);
  • 重写以下方法用于接收返回值
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 100) {
        ToastUtil.showTip(this, "lin = " + data.getStringExtra("lin"));
    }
}

相关文章

网友评论

    本文标题:打开其他app的Activity界面并获取返回值

    本文链接:https://www.haomeiwen.com/subject/rueppxtx.html