android developer tiny share-20160901

本文介绍如何使用Android的Intent机制通过WebView加载网页,并演示了如何配置Intent过滤器来响应特定URL。此外,还提供了使用adb工具验证应用是否正确响应Intent的方法。

今天讲使用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.


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值