拦截来自浏览器的链接以打开我的 Android 应用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1609573/
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
Intercepting links from the browser to open my Android app
提问by jamesh
I'd like to be able to prompt my app to open a link when user clicks on an URL of a given pattern instead of allowing the browser to open it. This could be when the user is on a web page in the browser or in an email client or within a WebView in a freshly-minted app.
我希望能够在用户点击给定模式的 URL 时提示我的应用程序打开一个链接,而不是允许浏览器打开它。这可能是当用户在浏览器或电子邮件客户端中的网页上或在新开发的应用程序中的 WebView 中时。
For example, click on a YouTube link from anywhere in the phone and you'll be given the chance to open the YouTube app.
例如,从手机的任意位置点击 YouTube 链接,您就有机会打开 YouTube 应用。
How do I achieve this for my own app?
我如何为我自己的应用程序实现这一目标?
回答by jamesh
Use an android.intent.action.VIEW of category android.intent.category.BROWSABLE.
使用 android.intent.action.VIEW 类别android.intent.category.BROWSABLE。
From Romain Guy's Photostreamapp's AndroidManifest.xml,
来自 Romain Guy 的Photostream应用程序的AndroidManifest.xml,
<activity
android:name=".PhotostreamActivity"
android:label="@string/application_name">
<!-- ... -->
<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="flickr.com"
android:pathPrefix="/photos/" />
<data android:scheme="http"
android:host="www.flickr.com"
android:pathPrefix="/photos/" />
</intent-filter>
</activity>
Once inside you're in the activity, you need to look for the action, and then do something with the URL you've been handed. The Intent.getData()
method gives you a Uri.
进入活动后,您需要查找操作,然后使用收到的 URL 执行某些操作。该Intent.getData()
方法为您提供了一个 Uri。
final Intent intent = getIntent();
final String action = intent.getAction();
if (Intent.ACTION_VIEW.equals(action)) {
final List<String> segments = intent.getData().getPathSegments();
if (segments.size() > 1) {
mUsername = segments.get(1);
}
}
It should be noted, however, that this app is getting a little bit out of date (1.2), so you may find there are better ways of achieving this.
然而,应该注意的是,这个应用程序有点过时了(1.2),所以你可能会发现有更好的方法来实现这一点。
回答by Cao Dongping
There are some libraries parse parameters from url automatically.
有一些库会自动从 url 解析参数。
such as
如
https://github.com/airbnb/DeepLinkDispatch
https://github.com/airbnb/DeepLinkDispatch
&&
&&
https://github.com/mzule/ActivityRouter
https://github.com/mzule/ActivityRouter
The later one is wrote by me. Which can parse parameters to given type, not always String.
后一篇是我写的。它可以将参数解析为给定类型,而不总是字符串。
Example
例子
@Router(value = "main/:id" intExtra = "id")
...
int id = getIntent().getInt("id", 0);
回答by Dacx
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
setUrlparams(url);
if (url.indexOf("pattern") != -1) {
// do something
return false;
} else {
view.loadUrl(url);
}
return true;
}
}