让一个链接在安卓浏览器启动我的应用程序?

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

<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>
279967 次浏览

我想你会想看看你的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方案开头的链接启动你的应用程序。

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

一种选择是拥有一个网站,并为该网站上的特定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>

或者,还有“意图:”方案。这允许你将几乎任何Intent描述为URI,浏览器将在单击时尝试启动该URI。要构建这样一个方案,最好的方法是只编写代码来构造你想要启动的Intent,然后打印Intent. touri (Intent. uri_intent_scheme)的结果。

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

如果你想确保它只启动你的应用程序,最好的方法是使用你自己的作用域动作,并使用Intent. setpackage()表示Intent只匹配你的应用程序包。

两者之间的权衡:

  • http uri要求您拥有自己的域。用户将始终获得在浏览器中显示URI的选项。它有很好的回退属性,如果你的应用没有安装,它们会直接登陆你的网站。

  • intent uri要求你的应用程序已经安装并且只在Android手机上安装。几乎允许任何意图(但总是包含BROWSABLE类别,并且不支持显式组件)。它们允许你只启动你的应用,而用户没有选择转向浏览器或任何其他应用。

试试我的简单技巧:

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

我的HTML链接:

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

或者你可以<a href="classRegister:真正的" > <-类文件名的"true"值

不过,这个脚本适用于mailto link:)

        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);
}

一旦你为你的应用设置了意图和自定义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.open(custom_url, '_blank')上为你抛出一个很好的异常,它也会失败并返回null,你可以稍后测试。

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

这是我的食谱:

创建一个静态HTML,重定向到你所请求的应用程序URL,把这个页面放在网页上。

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

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

一旦启动并运行了它,就可以使用上面建议的所有奇特功能来增强HTML。

这适用于内置浏览器Opera和Firefox(还没有测试过任何其他浏览器)。Firefox会提示“这个链接需要用应用程序打开”(好的,取消)。其他浏览器显然不太担心安全性,他们只是打开应用程序,没有任何问题。

我很抱歉推销自己,但我有一个jQuery插件启动本地应用程序从网页链接https://github.com/eusonlito/jquery.applink

你可以简单地使用它:

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


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

我也遇到过这个问题,看到过很多荒谬的页面。我了解到,要让你的应用程序可浏览,改变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>

这招对我很管用,也许也能帮到你。

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

https://github.com/airbnb/DeepLinkDispatch

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

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

它还可以处理查询参数。

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

<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,这应该是你的应用程序。

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

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

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