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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2958701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:02:54  来源:igfitidea点击:

Launch custom android application from android browser

androidandroid-intentintentfilter

提问by Parimal Modi

Can anybody please guide me regarding how to launch my android application from the android browser?

有人可以指导我如何从 android 浏览器启动我的 android 应用程序吗?

回答by Felix

Use an <intent-filter>with a <data>element. For example, to handle all links to twitter.com, you'd put this inside your <activity>in your AndroidManifest.xml:

将 an<intent-filter><data>元素一起使用。例如,要处理到 twitter.com 的所有链接,您可以将其放入<activity>您的AndroidManifest.xml:

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

Then, when the user clicks on a link to twitter in the browser, they will be asked what application to use in order to complete the action: the browser or your application.

然后,当用户在浏览器中点击 twitter 的链接时,他们将被询问使用什么应用程序来完成操作:浏览器或您的应用程序。

Of course, if you want to provide tight integration between your website and your app, you can define your own scheme:

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

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

Then, in your web app you can put links like:

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

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

And when the user clicks it, your app will be launched automatically (because it will probably be the only one that can handle my.special.scheme://type of uris). The only downside to this is that if the user doesn't have the app installed, they'll get a nasty error. And I'm not sure there's any way to check.

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



Edit:To answer your question, you can use getIntent().getData()which returns a Uriobject. You can then use Uri.*methods to extract the data you need. For example, let's say the user clicked on a link to http://twitter.com/status/1234:

编辑:要回答您的问题,您可以使用getIntent().getData()which 返回一个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"

You can do the above anywhere in your Activity, but you're probably going to want to do it in onCreate(). You can also use params.size()to get the number of path segments in the Uri. Look to javadoc or the android developer website for other Urimethods you can use to extract specific parts.

您可以ActivityonCreate(). 您还可以使用params.size()获取Uri. 查看 javadoc 或 android 开发者网站,了解Uri可用于提取特定部分的其他方法。

回答by AndroidGecko

All above answers didn't work for me with CHROMEas of 28 Jan 2014

CHROME截至 2014 年 1 月 28 日,上述所有答案均对我不起作用

my App launched properly from http://example.com/someresource/links from apps like hangouts, gmail etc but not from within chrome browser.

我的应用程序从http://example.com/someresource/链接从视频群聊、gmail 等应用程序中正确启动,但不能从 chrome 浏览器中启动。

to solve this, so that it launches properly from CHROME you have to set intent filter like this

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

<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>

note the pathPrefixelement

注意pathPrefix元素

your app will now appear inside activity picker whenever user requests http://example.com/someresource/pattern from chrome browser by clicking a link from google search results or any other website

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

回答by hackbod

Please see my comment here: Make a link in the Android browser start up my app?

请在此处查看我的评论:在 Android 浏览器中创建链接启动我的应用程序?

We strongly discourage people from using their own schemes, unless they are defining a new world-wide internet scheme.

我们强烈反对人们使用自己的方案,除非他们正在定义一个新的全球互联网方案。

回答by Zaki

In my case I had to set two categories for the <intent-filter>and then it worked:

就我而言,我必须为 设置两个类别<intent-filter>,然后它才起作用:

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

回答by Denshov

For example, You have next things:

例如,你有接下来的事情:

A link to open your app: http://example.com

The package name of your app: com.example.mypackage

用于打开您的应用的链接:http: //example.com

您的应用程序的包名称:com.example.mypackage

Then you need to do next:

那么接下来你需要做的是:

1) Add an intent filter to your Activity (Can be any activity you want. For more info check the documentation).

1)为您的活动添加一个意图过滤器(可以是您想要的任何活动。有关更多信息,请查看文档)。

        <activity
        android:name=".MainActivity">

        <intent-filter android:label="@string/filter_title_view_app_from_web">
            <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://example.com" -->

            <data
                android:host="example.com"
                android:scheme="http" />
        </intent-filter>

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity> 

2) Create a HTML file to test the link or use this methods.

2) 创建一个 HTML 文件来测试链接或使用此方法。

<a href="intent://example.com#Intent;scheme=http;package=com.example.mypackage;end">Open your Activity directly (just open your Activity, without a choosing dialog). </a>

<a href="http://example.com">Open this link with browser or your programm (by choosing dialog).</a>

3) Use Mobile Chrome to test

3) 使用 Mobile Chrome 进行测试

4) That's it.

4)就是这样。

And its not necessary to publish app in market to test deep linking =)

而且没有必要在市场上发布应用程序来测试深层链接 =)

Also, for more information, check documentationand useful presentation.

此外,有关更多信息,请查看文档和有用的演示文稿

回答by georgij

There should also be <category android:name="android.intent.category.BROWSABLE"/>added to the intent filter to make the activity recognized properly from the link.

还应该将其<category android:name="android.intent.category.BROWSABLE"/>添加到意图过滤器中,以便从链接正确识别活动。

回答by Varun Bhatia

The following link gives information on launching the app (if installed) directly from browser. Otherwise it directly opens up the app in play store so that user can seamlessly download.

以下链接提供有关直接从浏览器启动应用程序(如果已安装)的信息。否则它会直接在 Play 商店中打开应用程序,以便用户可以无缝下载。

https://developer.chrome.com/multidevice/android/intents

https://developer.chrome.com/multidevice/android/intents

回答by Zoltan

Please note if your icon is disappear from android launcher when you implement this feature, than you have to split intent-filter.

请注意,当您实现此功能时,如果您的图标从 android 启动器中消失,那么您必须拆分意图过滤器。

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <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:scheme="your-own-uri" />
        </intent-filter>
    </activity>

回答by RRK

Yeah, Chrome searches instead of looking for scheme. If you want to launch your App through URI scheme, use this cool utility App on the Play store. It saved my day :) https://play.google.com/store/apps/details?id=com.naosim.urlschemesender

是的,Chrome 搜索而不是寻找方案。如果您想通过 URI 方案启动您的应用程序,请在 Play 商店中使用这个很酷的实用程序应用程序。它拯救了我的一天 :) https://play.google.com/store/apps/details?id=com.naosim.urlschemesender

回答by Mehdi Dehghani

Xamarinport of Felix's answer

FelixXamarin端口的答案

In your MainActivity, add this (docs: Android.App.IntentFilterAttribute Class):

在您的 中MainActivity,添加以下内容(文档:Android.App.IntentFilterAttribute Class):

....
[IntentFilter(new[] { 
    Intent.ActionView }, 
    Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, 
    DataScheme = "my.special.scheme")
]
public class MainActivity : Activity
{
    ....

Xamarin will add following in the AndroidManifest.xmlfor you:

Xamarin 将为您添加以下内容AndroidManifest.xml

<activity android:label="Something" android:screenOrientation="portrait" android:theme="@style/MyTheme" android:name="blahblahblah.MainActivity">
  <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:scheme="my.special.scheme" />
  </intent-filter>
</activity>

And in order to get params (I tested in OnCreate of MainActivity):

为了获得参数(我在 MainActivity 的 OnCreate 中进行了测试):

var data = Intent.Data;
if (data != null)
{
    var scheme = data.Scheme;
    var host = data.Host;
    var args = data.PathSegments;

    if (args.Count > 0)
    {
        var first = args[0];
        var second = args[1];
        ...
    }
}

As far as I know, above can be added in any activity, not only MainActivity

据我所知,上面可以添加到任何活动中,不仅是 MainActivity

Notes:

笔记:

  1. When user click on the link, Android OS relaunch your app (kill prev instance, if any, and run new one), means the OnCreateevent of app's MainLauncher Activitywill be fired again.
  2. With this link: <a href="my.special.scheme://host/arg1/arg2">, in above lastcode snippet values will be:
  1. 当用户点击链接时,Android 操作系统会重新启动您的应用程序(杀死上一个实例,如果有,然后运行新的实例),这意味着OnCreate应用程序的事件MainLauncher Activity将再次被触发。
  2. 通过此链接:<a href="my.special.scheme://host/arg1/arg2">,在上面的最后一个代码片段中,值将是:
scheme: my.special.scheme
host: host
args: ["arg1", "arg2"]
first: arg1
second: arg2