使用Deep Link打开另一个应用程序时返回结果

时间:2016-06-29 05:20:28

标签: android

我有2个应用程序,A& B.在App A中,我使用Deep Link和startActivityForResult()打开App B的活动,但是当在App B中设置结果代码并返回时,App A刚收到RESULT_CANCELED!我的第一个问题是“使用Deep Link打开另一个应用程序时是否可以返回结果?”,如果是,我的错误在哪里?!

我的清单,在App 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>

在App 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);
        }
    }
}

最后,在App 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);

更新: Intent data中的onActivityResult()null !!!

2 个答案:

答案 0 :(得分:0)

更改您的App 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);

您的初始代码正在做的是您正在设置&#34; ID&#34;设置结果后额外。您应该在setResult()方法之前执行此操作。否则意图的更改不会影响结果。 首先将Extra值设置为Intent,然后使用setResult()方法。

答案 1 :(得分:0)

将下面的Intent Filter放在Manifest文件中,用于您正在使用“onActivityResult”方法的活动,然后您就可以从Othe App接收数据。

<intent-filter>
     <action android:name="android.intent.action.SEND" />
     <category android:name="android.intent.category.DEFAULT" />
     <data android:mimeType="text/plain" />
</intent-filter>

希望这有助于你