编程语言
首页 > 编程语言> > android – 使用深层链接从另一个启动应用程序

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

作者:互联网

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

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

解决方法:

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

标签:android,android-intent,deep-linking
来源: https://codeday.me/bug/20190608/1195845.html