今天讲使用intent实现通过WebView打开一个网页,另外讲下如何通过adb命令来查看一个app支持哪些intent-filter。
Web Browser
Load a web URL
To open a web page, use the ACTION_VIEW action and specify the web URL in the intent data.
Action
ACTION_VIEW
Data URI Scheme
http:<URL>
https:<URL>
MIME Type
"text/plain"
"text/html"
"application/xhtml+xml"
"application/vnd.wap.xhtml+xml"
Example 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);
}
}Example intent filter:
<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>Tip: If your Android app provides functionality similar to your web site, include an intent filter for URLs that point to your web site. Then, if users have your app installed, links from emails or other web pages pointing to your web site open your Android app instead of your web page.
Verify Intents with the Android Debug Bridge
To verify that your app responds to the intents that you want to support, you can use the adb tool to fire specific intents:
<1>Set up an Android device for development, or use a virtual device.
<2>Install a version of your app that handles the intents you want to support.
<3>Fire an intent using adb:
adb shell am start -a <ACTION> -t <MIME_TYPE> -d <DATA> \
-e <EXTRA_NAME> <EXTRA_VALUE> -n <ACTIVITY>For example:
adb shell am start -a android.intent.action.DIAL \
-d tel:555-5555 -n org.example.MyApp/.MyActivity<4>If you defined the required intent filters, your app should handle the intent.
For more information, see ADB Shell Commands.
本文介绍如何使用Android的Intent机制通过WebView加载网页,并演示了如何配置Intent过滤器来响应特定URL。此外,还提供了使用adb工具验证应用是否正确响应Intent的方法。
8976

被折叠的 条评论
为什么被折叠?



