从android浏览器启动自定义的android应用程序

任何人都可以请指导我如何从Android浏览器启动我的Android应用程序?


使用<intent-filter><data>元素。 例如,要处理到twitter.com的所有链接,您需要将它放在您的AndroidManifest.xml <activity>中:

<intent-filter>
    <data android:scheme="http" android:host="twitter.com"/>
    <action android:name="android.intent.action.VIEW" />
</intent-filter>

然后,当用户点击浏览器中的twitter链接时,系统会询问他们要完成操作的应用程序:浏览器或应用程序。

当然,如果你想提供你的网站和你的应用程序之间的紧密集成,你可以定义你自己的方案:

<intent-filter>
    <data android:scheme="my.special.scheme" />
    <action android:name="android.intent.action.VIEW" />
</intent-filter>

然后,在您的网络应用程序中,您可以放置​​如下链接:

<a href="my.special.scheme://other/parameters/here">

当用户点击它时,你的应用程序将自动启动(因为它可能是唯一可以处理my.special.scheme://类型的uris)。 唯一的缺点是,如果用户没有安装应用程序,他们会得到一个令人讨厌的错误。 我不确定有什么方法可以检查。


编辑:要回答你的问题,你可以使用getIntent().getData() ,它返回一个Uri对象。 然后可以使用Uri.*方法来提取所需的数据。 例如,假设用户点击了http://twitter.com/status/1234的链接:

Uri data = getIntent().getData();
String scheme = data.getScheme(); // "http"
String host = data.getHost(); // "twitter.com"
List<String> params = data.getPathSegments();
String first = params.get(0); // "status"
String second = params.get(1); // "1234"

你可以在Activity任何地方做上述事情,但你可能会想在onCreate()做到这一点。 您也可以使用params.size()来获取Uri的路径段数。 查看javadoc或android开发人员网站,了解可用于提取特定部分的其他Uri方法。


请在这里看到我的评论:在Android浏览器中创建一个链接启动我的应用程序?

我们强烈不鼓励人们使用他们自己的计划,除非他们正在界定一个新的全球互联网计划。


截至2014年1月28日,以上所有答案都不适用于我的CHROME

我的应用程序可以从http://example.com/someresource/从环聊,Gmail等应用程序中正确启动,但不能从Chrome浏览器中启动。

要解决这个问题,以便它从CHROME正确启动,您必须像这样设置intent过滤器

        <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="example.com"
                android:pathPrefix="/someresource/"
                android:scheme="http" />
            <data
                android:host="www.example.com"
                android:pathPrefix="/someresource/"
                android:scheme="http" />
        </intent-filter>

注意pathPrefix元素

无论何时用户通过点击从谷歌搜索结果或任何其他网站的链接请求http://example.com/someresource/来自Chrome浏览器的模式,您的应用现在都会出现在活动选择器中

链接地址: http://www.djcxy.com/p/31957.html

上一篇: Launch custom android application from android browser

下一篇: Query if Android database exists!