在 Android 浏览器中创建链接以启动我的应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3469908/
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 10:19:06  来源:igfitidea点击:

Make a link in the Android browser start up my app?

android

提问by Anton

Is it possible to make a link such as:

是否可以建立一个链接,例如:

<a href="anton://useful_info_for_anton_app">click me!</a>

cause my Anton app to start up?

导致我的 Anton 应用程序启动?

I know that this works for the Android Market app with the market protocol, but can something similar be done with other apps?

我知道这适用于具有市场协议的 Android Market 应用程序,但是其他应用程序可以做类似的事情吗?

Here is an example of a link that will start up the Android Market:

以下是启动 Android Market 的链接示例:

<a href="market://search?q=pname:com.nytimes.android">click me!</a>

Update:The answer I accepted provided by eldarerathis works great, but I just want to mention that I had some trouble with the order of the subelements of the <intent-filter>tag. I suggest you simply make another <intent-filter>with the new subelements in that tag to avoid the problems I had. For instance my AndroidManifest.xmllooks like this:

更新:我接受的 eldarerathis 提供的答案很好用,但我只想提一下,我在<intent-filter>标记子元素的顺序方面遇到了一些问题。我建议您简单地<intent-filter>使用该标签中的新子元素制作另一个,以避免我遇到的问题。例如我AndroidManifest.xml看起来像这样:

<activity android:name=".AntonWorld"
          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>
        <data android:scheme="anton" />
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

采纳答案by eldarerathis

I think you'll want to look at the <intent-filter>element of your Mainfest file. Specifically, take a look at the documentation for the <data>sub-element.

我想您会想要查看<intent-filter>Mainfest 文件的元素。具体来说,请查看<data>子元素的文档。

Basically, what you'll need to do is define your own scheme. Something along the lines of:

基本上,您需要做的是定义自己的方案。类似的东西:

<intent-filter>
    <data android:scheme="anton" />
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" /> <--Not positive if this one is needed
    ...
</intent-filter>

Then you should be able to launch your app with links that begin with the anton:URI scheme.

然后,您应该能够使用以anton:URI 方案开头的链接启动您的应用程序。

回答by hackbod

Please DO NOT use your own custom scheme like that!!! URI schemes are a network globalnamespace. Do you own the "anton:" scheme world-wide? No? Then DON'T use it.

请不要像那样使用您自己的自定义方案!!!URI 方案是一个网络全局命名空间。您在全球拥有“anton:”计划吗?不?那就别用了。

One option is to have a web site, and have an intent-filter for a particular URI on that web site. For example, this is what Market does to intercept URIs on its web site:

一种选择是拥有一个网站,并为该网站上的特定 URI 设置一个意图过滤器。例如,这是 Market 在其网站上拦截 URI 的做法:

        <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="http" android:host="market.android.com"
                android:path="/search" />
        </intent-filter>

Alternatively, there is the "intent:" scheme. This allows you to describe nearly any Intent as a URI, which the browser will try to launch when clicked. To build such a scheme, the best way is to just write the code to construct the Intent you want launched, and then print the result of intent.toUri(Intent.URI_INTENT_SCHEME).

或者,还有“意图:”方案。这允许您将几乎任何 Intent 描述为 URI,浏览器将在单击时尝试启动。构建这样的方案,最好的办法就是直接写代码构造你要启动的Intent,然后打印intent.toUri(Intent.URI_INTENT_SCHEME)的结果。

You can use an action with this intent for to find any activity supporting that action. The browser will automatically add the BROWSABLE category to the intent before launching it, for security reasons; it also will strip any explicit component you have supplied for the same reason.

您可以使用具有此意图的操作来查找支持该操作的任何活动。出于安全原因,浏览器会在启动之前自动将 BROWSABLE 类别添加到意图中;出于同样的原因,它还将删除您提供的任何显式组件。

The best way to use this, if you want to ensure it launches only your app, is with your own scoped action and using Intent.setPackage() to say the Intent will only match your app package.

如果你想确保它只启动你的应用程序,最好的方法是使用你自己的作用域操作并使用 Intent.setPackage() 来说明 Intent 将只匹配你的应用程序包。

Trade-offs between the two:

两者的取舍:

  • http URIs require you have a domain you own. The user will always get the option to show the URI in the browser. It has very nice fall-back properties where if your app is not installed, they will simply land on your web site.

  • intent URIs require that your app already be installed and only on Android phones. The allow nearly any intent (but always have the BROWSABLE category included and not supporting explicit components). They allow you to direct the launch to only your app without the user having the option of instead going to the browser or any other app.

  • http URI 要求您拥有自己的域。用户将始终可以选择在浏览器中显示 URI。它具有非常好的后备属性,如果您的应用程序未安装,它们将直接登陆您的网站。

  • Intent URI 要求您的应用程序已经安装并且只能在 Android 手机上安装。几乎允许任何意图(但始终包含 BROWSABLE 类别并且不支持显式组件)。它们允许您将启动定向到您的应用程序,而用户无法选择转到浏览器或任何其他应用程序。

回答by Lito

I apologize for promoting myself, but I have a jQuery plugin to launch native apps from web links https://github.com/eusonlito/jquery.applink

我为宣传自己而道歉,但我有一个 jQuery 插件可以从 Web 链接启动本机应用程序https://github.com/eusonlito/jquery.applink

You can use it easy:

您可以轻松使用它:

<script>
$('a[data-applink]').applink();
</script>

<a href="https://facebook.com/me" data-applink="fb://profile">My Facebook Profile</a>

回答by ashwani

I also faced this issue and see many absurd pages. I've learned that to make your app browsable, change the order of the XML elements, this this:

我也遇到了这个问题,看到了很多荒谬的页面。我了解到,要使您的应用程序可浏览,请更改 XML 元素的顺序,如下所示:

<activity
    android:name="com.example.MianActivityName"
    android:label="@string/title_activity_launcher">

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

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

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

    </intent-filter>
</activity>

This worked for me and might help you.

这对我有用,可能对你有帮助。

回答by JRun

Here's my recipe:

这是我的食谱:

Create a static HTML that redirects to your requested app URL, put that page on the web.

创建一个重定向到您请求的应用程序 URL 的静态 HTML,将该页面放在网络上。

That way, the links you share are 'real' links as far as Android is concerned ( they will be 'clickable').

这样,就 Android 而言,您共享的链接是“真实”链接(它们将是“可点击的”)。

You 'share' a regular HTTP link, www.your.server.com/foo/bar.html This URL returns a simple 8 line HTML that redirects to your app's URI (window.location = "blah://kuku") (note that 'blah' doesn't have to be HTTP or HTTPS any more).

您“共享”了一个常规的 HTTP 链接,www.your.server.com/foo/bar.html 这个 URL 返回一个简单的 8 行 HTML,重定向到您的应用程序的 URI (window.location = "blah://kuku") (请注意,“等等”不再必须是 HTTP 或 HTTPS)。

Once you get this up and running, you can augment the HTML with all the fancy capabilities as suggested above.

一旦你启动并运行它,你就可以用上面建议的所有花哨的功能来扩充 HTML。

This works with the built-in browser, Opera, and Firefox (haven't tested any other browser). Firefox asks 'This link needs to be opened with an application' (ok, cancel). Other browsers apparently don't worry about security that much, they just open the app, no questions asked.

这适用于内置浏览器、Opera 和 Firefox(尚未测试任何其他浏览器)。Firefox 询问“需要使用应用程序打开此链接”(好的,取消)。其他浏览器显然不太担心安全性,他们只是打开应用程序,不问任何问题。

回答by Pete

Once you have the intent and custom url scheme for your app set up, this javascript code at the top of a receiving page has worked for me on both iOS and Android:

一旦你为你的应用设置了意图和自定义 url 方案,接收页面顶部的这段 javascript 代码在 iOS 和 Android 上都对我有用:

<script type="text/javascript">
// if iPod / iPhone, display install app prompt
if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i) ||
    navigator.userAgent.match(/android/i)) {
  var store_loc = "itms://itunes.com/apps/raditaz";
  var href = "/iphone/";
  var is_android = false;
  if (navigator.userAgent.match(/android/i)) {
    store_loc = "https://play.google.com/store/apps/details?id=com.raditaz";
    href = "/android/";
    is_android = true;
  }
  if (location.hash) {
    var app_loc = "raditaz://" + location.hash.substring(2);
    if (is_android) {
      var w = null;
      try {
        w = window.open(app_loc, '_blank');
      } catch (e) {
        // no exception
      }
      if (w) { window.close(); }
      else { window.location = store_loc; }
    } else {
      var loadDateTime = new Date();
      window.setTimeout(function() {
        var timeOutDateTime = new Date();
        if (timeOutDateTime - loadDateTime < 5000) {
          window.location = store_loc;
        } else { window.close(); }
      },
      25);
      window.location = app_loc;
    }
  } else {
    location.href = href;
  }
}
</script>

This has only been tested on the Android browser. I am not sure about Firefox or Opera. The key is even though the Android browser will not throw a nice exception for you on window.open(custom_url, '_blank'), it will fail and return nullwhich you can test later.

这仅在 Android 浏览器上测试过。我不确定 Firefox 或 Opera。关键是即使 Android 浏览器不会在 上为您抛出一个很好的异常window.open(custom_url, '_blank'),它也会失败并返回null,您可以稍后进行测试。

Update: using store_loc = "https://play.google.com/store/apps/details?id=com.raditaz";to link to Google Play on Android.

更新:store_loc = "https://play.google.com/store/apps/details?id=com.raditaz";用于链接到 Android 上的 Google Play。

回答by Christian

You may want to consider a library to handle the deep link to your app:

您可能需要考虑使用一个库来处理指向您应用的深层链接:

https://github.com/airbnb/DeepLinkDispatch

https://github.com/airbnb/DeepLinkDispatch

You can add the intent filter on an annotated Activity like people suggested above. It will handle the routing and parsing of parameters for all of your deep links. For example, your MainActivity might have something like this:

您可以像上面建议的那样在带注释的 Activity 上添加意图过滤器。它将处理所有深层链接的参数路由和解析。例如,您的 MainActivity 可能具有以下内容:

@DeepLink("somePath/{useful_info_for_anton_app}")
public class MainActivity extends Activity {
   ...
}

It can also handle query parameters as well.

它还可以处理查询参数。

回答by IRvanFauziE

Try my simple trick:

试试我的简单技巧:

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(url.startsWith("classRegister:")) {                  
                Intent MnRegister = new Intent(getApplicationContext(), register.class); startActivity(MnRegister);
            }               
            view.loadUrl(url);
            return true;
        }

and my html link:

和我的 html 链接:

<a href="classRegister:true">Go to register.java</a>

or you can make < a href="classRegister:true" > <- "true" value for class filename

或者你可以做的<a href =“classRegister:真正”> <! - “真”的类文件名值

however this script work for mailto link :)

但是这个脚本适用于mailto链接:)

        if (url.startsWith("mailto:")) {
            String[] blah_email = url.split(":");
            Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
            emailIntent.setType("text/plain");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{blah_email[1]});
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, what_ever_you_want_the_subject_to_be)");
            Log.v("NOTICE", "Sending Email to: " + blah_email[1] + " with subject: " + what_ever_you_want_the_subject_to_be);
            startActivity(emailIntent);
        }