如需打开网页,请使用 ACTION_VIEW
操作,并在 Intent 数据中指定网址。
-
操作
-
ACTION_VIEW
数据 URI 架构
-
http:<URL>
https:<URL>
MIME 类型
-
"text/plain"
"text/html"
"application/xhtml+xml"
"application/vnd.wap.xhtml+xml"
示例 Intent:
public void openWebPage(String url) {
Uri webpage = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
示例 Intent 过滤器:
<activity ...>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<!-- Include the host attribute if you want your app to respond
only to URLs with your app's domain. -->
<data android:scheme="http" android:host="www.example.com" />
<category android:name="android.intent.category.DEFAULT" />
<!-- The BROWSABLE category is required to get links from web pages. -->
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
提示 :如果您的 Android 应用提供与您的网站相似的功能,请为指向您的网站的 URL 加入一个 Intent 过滤器。 之后,如果用户安装了您的应用,点击电子邮件或其他网页中指向您的网站的链接时,将会打开您的 Android 应用而不是您的网页。