安卓自定义URL以像iOS一样打开应用程序

31

我可以在网站上添加一个链接,例如'navigon://',在iOS设备上,如果安装了Navigon应用程序,则会打开Navigon应用程序。

在Android中是否有类似的简单方法来打开已安装的应用程序从网站上?


1
在这里回答:https://dev59.com/a3RB5IYBdhLWcg3wv5tA 和这里:https://dev59.com/CHE95IYBdhLWcg3wHqQV - pawelzieba
嗨@MaFt,我和你一样有同样的问题。你找到解决方案了吗? - Konrad Krakowiak
我在我的Android应用程序中使用Intent Filter完成了这个功能,现在的问题是电子邮件中使用的URL链接被重定向到另一个我需要的链接。而且初始链接无法被检测到。我该如何解决这个问题? - arslan haktic
希望这能帮到你或其他人 http://stackoverflow.com/questions/40781149/app-link-not-working-for-facebook-on-android/40781465#40781465 - Naeem Ibrahim
3个回答

21

Android深度链接:在你的清单文件中添加一个意图过滤器

<activity
android:name="com.example.android.GizmosActivity"
android:label="@string/title_gizmos" >
<intent-filter android:label="@string/filter_title_viewgizmos">
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <!-- Accepts URIs that begin with "http://www.example.com/gizmos” -->
    <data android:scheme="http"
          android:host="www.example.com"
          android:pathPrefix="/gizmos" />
    <!-- note that the leading "/" is required for pathPrefix-->
    <!-- Accepts URIs that begin with "example://gizmos”
    <data android:scheme="example"
          android:host="gizmos" />
    -->
</intent-filter>

https://developer.android.com/training/app-indexing/deep-linking.html


我该如何从中获取完整的URL(我正在尝试从URI中获取参数)? - jeet.chanchawat
1
您可以使用 intent.getDataString() 获取完整的URL。参考 https://dev59.com/x3I-5IYBdhLWcg3w6tJR#1609662 - SHS
这仅适用于http/https协议。它不适用于自定义协议,例如navigon://,如@Tim Castelijns的问题所述。 - Albert
<!--请注意,路径前缀需要带有"/"-->非常重要,我正在尝试在Postman中进行URL API测试,并想知道为什么同样的URL在stripe-terminal-android-app中无法打开。 - GD- Ganesh Deshmukh

6

2
谢谢,但那是指使用Android SDK编写应用程序的代码。我需要一种从网页“a”链接打开应用程序的方法,例如<a href="navigon://">打开Navigon</a>,如果在iOS设备上安装了Navigon应用程序,则会打开Navigon应用程序。 - MaFt
哦,当然有方法,我一回到家就告诉你。现在我得走了。 - Aman Alam
以下是如何在Android设备上调用Google应用程序的方法:http://developer.android.com/guide/appendix/g-app-intents.html,不过,以下链接也值得一读。 - Aman Alam

1

请查看实现iOS和Android移动深度链接所需的一切。这是一篇适用于Android和iOS的好文章。

您可能已经知道有深度链接,从Android 6.0开始出现了Android应用链接。后者旨在仅在您的应用程序中打开URL,而不是任何其他竞争对手。例如,如果不使用此验证,reddit.com可以在7个应用程序中打开。

enter image description here

您可以将每个需要的Activity与应该打开它的链接关联起来。例如,如果您希望在应用程序中打开像https://awesomejobs.com/jobs/{id}这样的链接,则需要将以下行添加到AndroidManifest.xml

<activity android:name="com.awesomejobsapp.ui.activity.JobActivity">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.BROWSABLE"/>
        <category android:name="android.intent.category.DEFAULT" />

        <data android:scheme="https"
              android:host="awesomejobs.com"
              android:pathPrefix="/jobs" />
    </intent-filter>
</activity>

然后在JobActivity中编写代码(从俄语的文章中接收到):

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.ac_job);

   final Intent intent = getIntent();
   final String action = intent.getAction();
   final String data = intent.getDataString();

   if (Intent.ACTION_VIEW.equals(action) && data != null) {
      final String jobId = data.substring(data.lastIndexOf("/") + 1);
      loadJobDetails(jobId);
   }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接