我有两个应用程序,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 data
为 null
!