使用深层链接从另一个启动应用程序

时间:2017-10-25 10:45:07

标签: android android-intent deep-linking

我正在处理两个应用程序A和B.

App B有这样的深层链接:myApp://open/myAction?param=123

看起来像:

<!-- Update myAction deep link -->
<intent-filter android:label="@string/launcherName">

    <action android:name="android.intent.action.VIEW" />

    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE />
    <data
        android:host="open/*"
        android:scheme="myApp" />
</intent-filter>

如果我使用adb启动应用程序,它可以完美运行。

现在,当用户点击活动A上的按钮时,我尝试启动aplicacion B.

单击按钮(OnClickListener)后,我尝试了这个(在GoodleDeveloper中找到)

// Build the intent
Uri myAction = Uri.parse(mEditText.getText().ToString()); // is something like: `myApp://open/myAction?param=1AD231XAs`
Intent mapIntent = new Intent(Intent.ACTION_VIEW, myAction);

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

// Start an activity if it's safe
if (isIntentSafe) {
    startActivity(mapIntent);
}

显然不起作用,但我不确定为什么不这样做。也许我错过了什么。

3 个答案:

答案 0 :(得分:4)

尝试从PackageManager创建Intent并在启动deepLink之前设置动作(ACTION_VIEW)和数据(myAction):

    Uri myAction = Uri.parse(mEditText.getText().toString());

    PackageManager packageManager = getPackageManager();
    Intent intent = packageManager.getLaunchIntentForPackage(<app_destination_package>);

    if (intent != null) {
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(myAction);
        startActivity(intent);
    }

答案 1 :(得分:3)

我不知道为什么上述答案被标记为正确答案,因为它只能打开屏幕定义为LAUNCHER的应用程序,而不能打开深层链接。

在此之后,您可以从应用XXX中打开所需的任何屏幕!

 private void startAppXXXfromThisFuckinApp() {
   // pass the uri (scheme & screen path) of a screen defined from app XXX that you want to open (e.g HomeActivity)
   Uri uri = Uri.parse("xxx://screen/home");
   Intent mapIntent = new Intent(Intent.ACTION_VIEW, uri);

  //Verify if app XXX has this screen path
    PackageManager packageManager = getPackageManager();
    List<ResolveInfo> activities = 
    packageManager.queryIntentActivities(mapIntent, 0);
    boolean isIntentSafe = activities.size() > 0;

   //Start HomeActivity of app XXX because it's existed
    if (isIntentSafe) {
        startActivity(mapIntent);
    }
 }

显然,在应用XXX中,AndroidManifest.xml必须是这样的:

<activity
 android:name=".HomeActivity"
 <intent-filter>
  <action android:name="android.intent.action.VIEW"/>
  <category android:name="android.intent.category.DEFAULT"/>
  <category android:name="android.intent.category.BROWSABLE"/>
  <data>
   android:host="screen/home"
   android:scheme="xxx" />
 </intent-filter>

女士们,先生们,它将通过应用XXX打开屏幕HomeActivity。!

答案 2 :(得分:2)

像这样更改你的清单

<data
    android:host="open"
    android:pathPattern="/myAction?param=123"
    android:scheme=" myApp" />

在第一个活动中发送意图

Intent intent = new Intent (Intent.ActionView);
intent.setData (Uri.Parse (DEEP_LINK_URL));

在你的第二个活动中

if(getIntent()!=null){
    Intent deepLink = getIntent();
    deepLink.getScheme();
    deepLink.getData().getPath();   
}