Android学习笔记(十八)——使用意图筛选器和实现浏览网页(附源代码)
使用意图筛选器
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、执行一下,效果例如以下:
点击第三个button:
![]()
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemVuYWlsNTAxMTI5/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
点击第一个button:
若想完好意图筛选器,则在IntentsActivity.java的onClickWebBrowser()方法中加入createChooser()方法:
startActivity(intent.createChooser(intent, "Open URL using..."));
加入后的效果例如以下:
这时就可以选择你想要选择的应用程序就可以~
附、使用createChooser()的优点:
1、将显示的选择对话框的标题改掉,且没有了Use by default for this action选项。
2、当没有活动与程序的Intent对象匹配时,应用程序不会崩溃。
Android学习笔记(十八)——使用意图筛选器和实现浏览网页(附源代码)的更多相关文章
- python3.4学习笔记(十八) pycharm 安装使用、注册码、显示行号和字体大小等常用设置
python3.4学习笔记(十八) pycharm 安装使用.注册码.显示行号和字体大小等常用设置Download JetBrains Python IDE :: PyCharmhttp://www. ...
- 【转】 Pro Android学习笔记(八八):了解Handler(2):什么是Handler
文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 之前我们有一篇很好的博文<Andro ...
- 【转】 Pro Android学习笔记(八二):了解Package(1):包和进程
文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 在之前,我们已经学习了如何签发apk,见P ...
- 【转】 Pro Android学习笔记(八十):服务(5):访问远程服务
目录(?)[-] Client的AIDL文件 Client的代码 建立连接 请求服务 断开连接 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://bl ...
- Android学习笔记(八)——四种基本布局
//此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 布局是一种可用于放置很多控件的容器,它可以按照一定的规律调整内部控件的位置,或是嵌套子布局,从而编写出精美的界 ...
- (C/C++学习笔记) 十八. 继承和多态
十八. 继承和多态 ● 继承的概念 继承(inheritance): 以旧类为基础创建新类, 新类包含了旧类的数据成员和成员函数(除了构造函数和析构函数), 并且可以派生类中定义新成员. 形式: cl ...
- 【转】 Pro Android学习笔记(八九):了解Handler(3):延迟执行小例子
目录(?)[-] 小例子 Handler的处理 Activity的代码片段 后台线程和UI的互动 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://b ...
- 【转】 Pro Android学习笔记(八六):了解Package(5):使用lib
目录(?)[-] 在项目中使用lib 源代码 了解一些机制 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowin ...
- 【转】 Pro Android学习笔记(八四):了解Package(3):包间数据共享
目录(?)[-] 共享User ID的设置 共享资源例子 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowing ...
随机推荐
- Java中接口的作用
转载于:https://www.zhihu.com/question/20111251 困惑:例如我定义了一个接口,但是我在继承这个接口的类中还要写接口的实现方法,那我不如直接就在这个类中写实现方法岂 ...
- 【Luogu】P4234最小差值生成树(LCT)
题目链接 能把LCT打得每个函数都恰有一个错误也是挺令我惊讶的. 本题使用LCT维护生成树,具体做法是对原图中的每个边建一个点,然后连边的时候相当于是将边的起点跟“边”这个点连起来,边的终点也跟它连起 ...
- [LOJ#114]k 大异或和
[LOJ#114]k 大异或和 试题描述 这是一道模板题. 给由 n 个数组成的一个可重集 S,每次给定一个数 k,求一个集合 T⊆S,使得集合 T 在 S 的所有非空子集的不同的异或和中,其异或和 ...
- html body width height 100%使用
首先我们来看一个实际的问题,让body中的一个div占全屏,(问题来源:http://stackoverflow.com/questions/1575141/make-div-100-height-o ...
- net5:自定义验证控件服务器端验证与客户端验证的使用
原文发布时间为:2008-07-29 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- BZOJ1001[BeiJing2006]狼抓兔子最小割網絡流
Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的, 而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一 ...
- erlang debugger
http://erlang.org/doc/apps/debugger/debugger_chapter.html
- Handler处理机制
handler缺点:如果要运送两种类型的数据(比如一个Bitmap,一个Object)就不能运送,但可以用Bunder来传输 * 使用handler的步骤: * 1.创建一个handl ...
- Unity -- 入门教程二
为了接下来要做的小游戏,在这里我要小小的修改一下移动的代码. public class PlayerMove : MonoBehaviour { //定义移动的速度 public float Move ...
- 为了安全,linux下如何使用某个用户启动某个进程?
安全里有个原则,叫最小权限原则 根据这个原则,对于启动某个应用或者进程,应该赋予其最小权限,根据应用权限要求,创建一个相应权限的用户,赋予其应用相应的权限,然后使用这个用户启用这个应用 如何使用某个用 ...