[英]How to launch an app using a deeplink in android
I want to launch app using my own app but not by giving the package name, I want to open a custom URL.我想使用我自己的应用程序启动应用程序,但不是通过提供程序包名称,我想打开一个自定义 URL。
I do this to start an application.我这样做是为了启动一个应用程序。
Intent intent = getPackageManager().getLaunchIntentForPackage(packageInfo.packageName);
startActivity(intent);
Instead of package name is it possible to give a deep-link for example:可以提供深层链接而不是包名称,例如:
"mobiledeeplinkingprojectdemo://product/123"
You need to define a activity that will subscribe to required intent filters:您需要定义一个将订阅所需意图过滤器的活动:
<activity
android:name="DeepLinkListener"
android:exported="true" >
<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="host"
android:pathPattern="some regex"
android:scheme="scheme" />
</intent-filter>
</activity>
Then in onCreate of your DeepLinkListener activity you can access the host,scheme etc:然后在您的 DeepLinkListener 活动的 onCreate 中,您可以访问主机、方案等:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent deepLinkingIntent= getIntent();
deepLinkingIntent.getScheme();
deepLinkingIntent.getData().getPath();
}
perform check on path and again fire a intent to take the user to corresponding activity.对路径执行检查并再次激发意图将用户带到相应的活动。 refer data for more help 参考数据以获得更多帮助
Now fire a Intent:现在触发一个意图:
Intent intent = new Intent (Intent.ACTION_VIEW);
intent.setData (Uri.parse(DEEP_LINK_URL));
Don't forget to handle the exception.不要忘记处理异常。 If there is no activity that can handle the deep link, startActivity will return an exception.如果没有可以处理深层链接的活动,则 startActivity 将返回异常。
try {
context.startActivity(
Intent(Intent.ACTION_VIEW).apply {
data = Uri.parse(deepLink)
}
)
} catch (exception: Exception) {
Toast.makeText(context, exception.localizedMessage, Toast.LENGTH_LONG).show()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.