我希望能够在用户单击给定模式的 URL 时提示我的应用打开链接,而不是允许浏览器打开它。这可能是当用户在浏览器中的网页上或在电子邮件客户端中或在新创建的应用程序中的 WebView 中时。
例如,从手机中的任意位置点击 YouTube 链接,您就有机会打开 YouTube 应用。
如何为我自己的应用实现这一点?
最佳答案
使用类别 android.intent.category.BROWSABLE 的 android.intent.action.VIEW .
来自 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>
进入activity ,你需要寻找 Action ,然后对你得到的 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);
}
}
但是应该注意,这个应用程序已经有点过时了(1.2),所以你可能会发现有更好的方法来实现这一点。
关于android - 从浏览器拦截链接以打开我的 Android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1609573/