如何在Android中打开深层链接uri? [英] How to open a deep linking uri in Android?

查看:208
本文介绍了如何在Android中打开深层链接uri?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,有一个uri example:// activityone?foo = bar ,它可以通过此adb命令打开应用程序并启动一项活动

For example, there is an uri example://activityone?foo=bar which can open up an application and launch one of the activity by this adb command

adb shell am start -W -a android.intent.action.VIEW -d "example://activityone?foo=bar" com.example.deeplinking

是否还有其他方法可以通过此uri启动android应用( example:// activityone ?foo = bar )?我如何将这个uri放入电子邮件中,然后单击它就会启动该应用程序?

Are there any other ways to launch the android app through this uri (example://activityone?foo=bar)? How can I put this uri in an email, and when it is clicked, it will launch the app?

推荐答案

首先,您应该阅读以下文档:意图和意图过滤器。特别是接收内隐意图部分。

First, you should read the documentation for this: Intents and Intent Filters. Specifically the section "Receiving an Implicit Intent".

如其他人所述,为此使用自定义方案会出现问题。

As others have stated, using a custom scheme for this has issues.


  • 它们并不总是被视为链接

  • 您并不像拥有域名(主机)那样拥有它们。

因此,您应该在清单中定义一个活动,并为 host 使用一个意图过滤器,并使用真实的方案。请参见的操作测试,类别测试和数据测试部分。意图与意向过滤器文档,以了解如何针对您的特定用例进行配置。

So you should define an activity in you Manifest with an intent filter for your host and use a real scheme. See the "Action Test", "Category Test" and "Data Test" sections of the Intent & Intent Filters documentation to see how to configure this for your specific use case.

<activity android:name="com.example.app.ActivityOne">
    <intent-filter>
        <data android:scheme="http"/>
        <data android:host="example.com"/>
        <data android:path="/activityone"/>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
    </intent-filter>
</activity>

然后,您将可以使用以下链接。

Then you will be able to use a link like the following.

http://example.com/activityone?foo=bar

由于这是一个http链接,系统将要求用户选择打开哪个应用程序(您的应用程序或他们偏好的浏览器)。如果他们选择了您的应用程序,它将启动ActivityOne,它可以像这样获取查询数据。

Since this is an http link the system will ask the user to choose which app to open it with (your app or their preferred browser). If they choose your app it will start ActivityOne which can get the query data like so.

public class ActivityOne extends Activity {

    @Override
    public void onResume() {
        super.onResume();
        Intent intent = getIntent();
        Uri uri = intent.getData();
        String foo = uri.getQueryParameter("foo");
    }

}

如果您在意图过滤器,那么您(很可能)将成为唯一注册为处理该意图的应用程序,而用户不必选择您的应用程序。但是,要在电子邮件中包含带有自定义方案的链接会更加困难。大多数电子邮件客户端无法将任何使用自定义方案的内容识别为链接,并且无法点击。

If you use a custom scheme in the intent filter then you will (most likely) be the only app registered to handle that intent and the user will not have to select your app. However, it will be much harder for you to include a link with a custom scheme in an email. Most email clients will not recognize anything with a custom scheme as a link and it will not be clickable.

这篇关于如何在Android中打开深层链接uri?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆