是否可以建立这样的链接:

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

让我的安东应用启动吗

我知道这适用于Android Market应用程序的市场协议,但类似的事情是否也适用于其他应用程序?

下面是一个将启动Android Market的链接示例:

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

更新: 我接受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>

当前回答

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

基本上,您需要做的就是定义自己的方案。大致如下:

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

然后你就可以用anton: URI方案开头的链接来启动你的应用了。

其他回答

这个方法不会调用消歧对话框,要求你打开应用程序或浏览器。 如果您在舱单中登记以下内容

<manifest package="com.myApp" .. >
  <application ...>
    <activity ...>
      <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="gallery"
          android:scheme="myApp" />
      </intent-filter>
    </activity>
    ..

例如,在手机上点击电子邮件中的这个url

<a href="intent://gallery?directLink=true#Intent;scheme=myApp;package=com.myApp;end"> 
  Click me 
</a>

然后android会尝试找到一个带有com包的应用程序。myApp响应你的画廊意图,并有一个myApp方案。万一它不能,它会带你去商店,寻找com。myApp,应该是你的app。

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

基本上,您需要做的就是定义自己的方案。大致如下:

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

然后你就可以用anton: URI方案开头的链接来启动你的应用了。

只想通过浏览器打开应用程序?你可以使用下面的代码来实现:

HTML:

<a href="intent:#Intent;action=packageName;category=android.intent.category.DEFAULT;category=android.intent.category.BROWSABLE;end">Click here</a>

清单:

<intent-filter>
      <action android:name="packageName" />
      <category android:name="android.intent.category.DEFAULT" />
      <category android:name="android.intent.category.BROWSABLE" />
</intent-filter>

这个意图过滤器应该在Launcher Activity中。

如果你想在点击浏览器链接时传递数据,只需引用这个链接。

一旦你为你的应用设置了意图和自定义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>

这个功能只在Android浏览器上测试过。Firefox和Opera我不太确定。关键是即使Android浏览器不会在window上为你抛出一个很好的异常。打开(custom_url, '_blank'),它将失败并返回null,您可以稍后进行测试。

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

你可能想要考虑一个库来处理你的应用程序的深层链接:

https://github.com/airbnb/DeepLinkDispatch

你可以像上面建议的那样在一个带注释的Activity上添加intent过滤器。它将处理所有深度链接的参数路由和解析。例如,你的MainActivity可能有这样的东西:

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

它还可以处理查询参数。