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

Launch application from another using a deeplink

我正在开发两个应用程序 A 和 B,我希望通过深层链接将它们相互链接。

应用 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 启动应用程序,它会完美运行。

现在,当用户单击 Activity A 中的按钮时,我正在尝试启动应用程序 B。

我在单击按钮 (OnClickListener)

时尝试了此操作(位于 GoogleDeveloper
// 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);
}

但是我无法使用此应用打开其他应用。

像这样更改您的清单

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

在第一个 activity

中发送意向
Intent intent = new Intent (Intent.ActionView);
intent.setData (Uri.Parse (DEEP_LINK_URL));

而在你的第二个 activity

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

尝试从 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);
    }

以上答案只能打开屏幕定义为 LAUNCHER 的应用程序,不能打开 deep link。

这将适用于 link 应用 XYZ 与任何应用:

 private void startAppXYZfromThisFuckinApp() {
   // 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("xyz://screen/home");
   Intent mapIntent = new Intent(Intent.ACTION_VIEW, uri);

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

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

显然,在应用程序 XYZ 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="xyz" />
 </intent-filter>
   

现在将从应用 XYZ 打开屏幕 HomeActivity!