(十八)——使用意图筛选器和实现浏览网页(附源码)
最后更新于:2022-04-01 20:17:09
**使用意图筛选器**
**[点击下载源码](http://download.csdn.net/detail/u012904198/7374025)**
1、创建一个Intents项目,给该项目添加一个新类,命名为MyBrowserActivity,在res/layout文件夹下新增一个browser.xml;
2、在AndroidManifest.xml文件中添加如下代码:
添加权限:
~~~
~~~
~~~
~~~
action:动作;category:类别;data:指明获取的数据类型。
3、在main.xml文件中添加三个Button:
~~~
~~~
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:
~~~
~~~
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对象匹配时,应用程序不会崩溃。
';