使用深度链接打开另一个应用程序时返回结果

4

我有两个应用程序,A和B。在应用程序A中,我使用深度链接打开了应用程序B的一个活动,并通过startActivityForResult(),但是当在App B中设置结果代码并返回时,App A只收到了RESULT_CANCELED!我的第一个问题是:“在使用深度链接打开另一个应用程序时是否可能返回结果?”,如果是的话,我的错误在哪里?!

我的清单,在应用程序B中:

<activity android:name=".activity.TargetActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <!-- Accepts URIs that begin with "http://www.example.com/gizmos” -->
        <!-- note that the leading "/" is required for pathPrefix-->
        <data
            android:host="www.example.com"
            android:pathPrefix="/gizmos"
            android:scheme="http" />
        <data
            android:host="www.example.com"
            android:pathPrefix="/gizmos"
            android:scheme="https" />
        <data
            android:host="example.com"
            android:pathPrefix="/gizmos"
            android:scheme="http" />
        <data
            android:host="example.com"
            android:pathPrefix="/gizmos"
            android:scheme="https" />
    </intent-filter>
</activity>

在应用程序A中:
            Uri webpage = Uri.parse("android-app://com.vancosys.payment/http/www.example.com/gizmos?name=FMarket&id=4231");

            Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);

            // Verify it resolves
            PackageManager    packageManager = getPackageManager();
            List<ResolveInfo> activities     = packageManager.queryIntentActivities(webIntent, 0);
            boolean           isIntentSafe   = activities.size() > 0;

            // Start an activity if it's safe
            if (isIntentSafe)
            {
                // '4231' is my request code
                startActivityForResult(webIntent, 4231);
            }

此外:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(requestCode == 4231)
    {
        if (resultCode == RESULT_OK)
        {
            String id = data.getStringExtra("ID");

            Intent i = new Intent(this, ResultActivity.class);
            i.putExtra("RESULT", true);
            i.putExtra("ID", id);
            startActivity(i);
        }
        else
        {
            // It JUST equals to RESULT_CANCELED!!!
            Intent i = new Intent(this, ResultActivity.class);
            i.putExtra("result", false);
            startActivity(i);
        }
    }
}

最后,在应用程序B中:
                        new Handler().postDelayed(new Runnable()
                        {
                            public void run()
                            {
                                Intent i = new Intent();
                                TargetActivity.this.setResult(RESULT_OK, i);
                                i.putExtra("ID", "45651232");
                                finish();
                            }
                        }, 3000);

更新:

onActivityResult() 中的 Intent datanull


1
你的问题有没有解决方案? - Rambo7
2个回答

0
将您的应用程序 B 代码更改如下:
 new Handler().postDelayed(new Runnable()
                    {
                        public void run()
                        {
                            Intent i = new Intent();
                            i.putExtra("ID", "45651232");
                            //setResult() method should be called after all the putExtra() methods
                            TargetActivity.this.setResult(RESULT_OK, i);

                            finish();
                        }
                    }, 3000);

你的初始代码所做的是在设置结果后设置 "ID" 额外信息。你应该在 setResult() 方法之前做这个操作,否则意图中的更改将不会影响结果。 首先将额外值设置到意图中,然后使用 setResult() 方法。


不行!没有起作用。返回的意图为空,结果代码为RESULT_CANCELED... - YUSMLE
检查此处理程序是否正在被调用... 检查是否存在任何可能导致应用程序B在不设置结果的情况下关闭的情况。 - Akhil Soman

0
在使用“onActivityResult”方法的活动的清单文件中放置以下意图过滤器,然后您就可以从其他应用程序接收数据。
<intent-filter>
     <action android:name="android.intent.action.SEND" />
     <category android:name="android.intent.category.DEFAULT" />
     <data android:mimeType="text/plain" />
</intent-filter>

希望这能对你有所帮助。

不行,我的 onActivityResult() 里仍然有 data == null - YUSMLE

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接