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

问题描述 投票:4回答:2

我正在研究两个应用程序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.

单击按钮时我尝试了这个(找到:GoodleDeveloper)(OnClickListener

// 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);
}

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

android android-intent deep-linking
2个回答
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);
    }

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();   
}

0
投票

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

按照这个你可以打开你想要的应用程序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);
    }
 }

显然,在app 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。

© www.soinside.com 2019 - 2024. All rights reserved.