简体   繁体   English

Andorid,如何使用自定义URL方案打开另一个应用程序?

[英]Andorid, how to open another app using custom URL scheme?

From my native application, I need to open my another app which build by Ionic. 从我的本机应用程序中,我需要打开由Ionic构建的另一个应用程序。

I tried like this, 我这样尝试过

val intent = packageManager.getLaunchIntentForPackage("com.example.app")
intent.putExtra("uri", "hello 2?")
intent.data = Uri.parse("hello?")
startActivity(intent)

It could launch the app but, it never called handleOpenURL() method. 它可以启动应用程序,但从未调用handleOpenURL()方法。

So I want to try using the custom URL scheme instead of the package name. 因此,我想尝试使用自定义URL方案而不是程序包名称。

How can I open another app using custom URL scheme with URI parameter? 如何使用带有URI参数的自定义URL方案打开另一个应用程序?

 Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setData(Uri.parse("YOUR_URL"));
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    } else {
        Toast.makeText(this, R.string.no_activity_found, Toast.LENGTH_LONG).show();
    }

This worked for me. 这对我有用。 In your android manifest's launcher activity mention the following lines 在您的Android清单的启动器活动中,提及以下几行

<intent-filter>
      <data android:scheme="http" />
      <!-- or you can use deep linking like  -->

      <data android:scheme="http" android:host="abc.xyz"/>
      <action android:name="android.intent.action.VIEW"/>
      <category android:name="android.intent.category.BROWSABLE"/>
      <category android:name="android.intent.category.DEFAULT"/>

</intent-filter>

Instead of this host name change to suit your preferences and you can use your own scheme as well. 可以根据自己的喜好更改主机名,而不用更改主机名,也可以使用自己的方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM