**使用意圖篩選器**
**[點擊下載源碼](http://download.csdn.net/detail/u012904198/7374025)**
1、創建一個Intents項目,給該項目添加一個新類,命名為MyBrowserActivity,在res/layout文件夾下新增一個browser.xml;
2、在AndroidManifest.xml文件中添加如下代碼:
添加權限:
~~~
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.INTERNET" />
~~~
~~~
<activity
android:name=".MyBrowserActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="net.zenail.MyBrowser" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
</intent-filter>
</activity>
~~~
action:動作;category:類別;data:指明獲取的數據類型。
3、在main.xml文件中添加三個Button:
~~~
<Button
android:id="@+id/btn_webbrowser"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickWebBrowser"
android:text="Web Browser" />
<Button
android:id="@+id/btn_makecalls"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickMakeCalls"
android:text="Make Calls" />
<Button
android:id="@+id/btn_launchMyBrowser"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickLaunchMyBrowser"
android:text="Launch My Browser" />
~~~
4、在IntentsActivity.java文件中添加三個Button對應的三個點擊方法:
~~~
public void onClickWebBrowser(View v) {
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://網址"));//此處輸入百度網址,CSDN不讓加鏈接...
//使用createChooser()的好處:
//1、將顯示的選擇對話框的標題改掉,且沒有了Use by default for this action選項
//2、當沒有活動與程序的Intent對象匹配時,應用程序不會崩潰
//startActivity(intent.createChooser(intent, "Open URL using..."));
startActivity(intent);
}
public void onClickMakeCalls(View v) {
Intent intent = new Intent(android.content.Intent.ACTION_DIAL,
Uri.parse("tel:+651234567"));
startActivity(intent);
}
public void onClickLaunchMyBrowser(View v) {
Intent intent = new Intent("net.zenail.MyBrowser");
intent.setData(Uri.parse("http://網址"));//此處輸入百度網址,CSDN不讓加鏈接...
startActivity(intent);
}
~~~
5、在browser.xml中添加一個WebView:
~~~
<WebView
android:id="@+id/WebView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
~~~
6、在MyBrowserActivity.java文件中添加如下代碼,實現瀏覽網頁功能:
~~~
public class MyBrowserActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.browser);
Uri url = getIntent().getData();
WebView webView = (WebView) findViewById(R.id.WebView01);
webView.setWebViewClient(new Callback());
webView.loadUrl(url.toString());
}
private class Callback extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
return false;
}
}
}
~~~
7、運行一下,效果如下:

點擊第三個按鈕:

點擊第一個按鈕:

若想完善意圖篩選器,則在IntentsActivity.java的onClickWebBrowser()方法中添加createChooser()方法:
startActivity(intent.createChooser(intent, "Open URL using..."));
添加后的效果如下:

這時即可選擇你想要選擇的應用程序即可~
附、使用createChooser()的好處:
1、將顯示的選擇對話框的標題改掉,且沒有了Use by default for this action選項;
2、當沒有活動與程序的Intent對象匹配時,應用程序不會崩潰。
- 前言
- Android應用程序剖析
- (一)——生命周期
- (二)——使用Intent傳數據之通用方式
- (三)——使用靜態變量傳遞數據
- (四)——通過剪切板傳遞數據
- (五)——通過全局變量傳遞數據
- (六)——從Activity返回數據
- adt-bundle-linux-x86_64-20131030下新建工程提示找不到adb和R.java問題的解決
- Eclipse啟動時提示fail to create the Java Virtual Machine問題的解決
- Android常見UI組件之ListView(一)
- Android常見UI組件之ListView(二)——定制ListView
- (七)——顯示對話框窗口
- (八)——顯示進度對話框
- (九)——更復雜的進度對話框
- (十)——使用意圖鏈接活動
- (十一)——從意圖返回結果
- (十二)——使用意圖傳遞數據的幾種方式
- (十三)——碎片(一)
- (十四)——在運行時添加碎片(附源碼)
- (十五)——碎片的生命周期(附源碼)
- (十六)——碎片之間進行交互(附源碼)
- (十七)——使用意圖調用內置應用程序
- (十八)——使用意圖篩選器和實現瀏覽網頁(附源碼)