Android 应用指定浏览器开发实例
作者:lqh 时间:2022-02-10 03:34:09
本文主要讲解Android浏览器的开发实例,有三部分内容:启动Android默认浏览器、指定浏览器进行访问以及打开本地的html文件。
一、启动Android默认浏览器
Java代码
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse("http://www.cnblogs.com");
intent.setData(content_url);
startActivity(intent);
这样子,android就可以调用起手机默认的浏览器访问。
二、指定浏览器进行访问
1、指定android自带的浏览器访问
(“com.android.browser”:packagename;“com.android.browser.BrowserActivity”:启动主activity)
Java代码
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse("http://www.jizhuomi.com/android");
intent.setData(content_url);
intent.setClassName("com.android.browser","com.android.browser.BrowserActivity");
startActivity(intent);
2、启动其他浏览器(当然该浏览器必须安装在机器上)
只要修改以下相应的packagename和主启动activity,即可调用其他浏览器。
intent.setClassName("com.android.browser","com.android.browser.BrowserActivity");
uc浏览器":"com.uc.browser", "com.uc.browser.ActivityUpdate“
opera:"com.opera.mini.android", "com.opera.mini.android.Browser"
qq浏览器:"com.tencent.mtt", "com.tencent.mtt.MainActivity"
三、打开本地html文件
打开本地的html文件的时候,一定要指定某个浏览器,而不能采用方式一来浏览,具体示例代码如下:
Java代码
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse("content://com.android.htmlfileprovider/sdcard/help.html");
intent.setData(content_url);
intent.setClassName("com.android.browser","com.android.browser.BrowserActivity");
startActivity(intent);
关键点是调用了"content”这个filter。
以前有在win32编程的朋友,可能会觉得用这种形式”file://sccard/help.html“是否可以,可以很肯定的跟你说,默认的浏览器设置是没有对"file“这个进行解析的,如果要让你的默认android浏览器有这个功能需要自己到android源码修改manifest.xml文件,然后自己编译浏览器代码生成相应的apk包来重新在机器上安装。
大体的步骤如下:
1、打开packages/apps/Browser/AndroidManifest.xml文件把加到相应的<intent-filter>后面就可以了。
XML/HTML代码
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="file" />
</intent-filter>
2、重新编译打包,安装,这样子,新的浏览器就支持"file”这个形式了。
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
关于Springboot数据库配置文件明文密码加密解密的问题
详解Java中LinkedHashMap
![](https://img.aspxhome.com/file/2023/1/95031_0s.png)
android Animation监听器AnimationListener的使用方法)
![](https://img.aspxhome.com/file/2023/4/94284_0s.jpg)
C#实现将DataTable内容输出到Excel表格的方法
![](https://img.aspxhome.com/file/2023/4/68084_0s.png)
java 实现定时的方法及实例代码
Java生成含字母和数字的6位随机字符串
C#利用com操作excel释放进程的解决方法
Springboot - Fat Jar示例详解
java之使用多线程代替for循环(解决主线程提前结束问题)
Java中StringBuilder与StringBuffer使用及源码解读
![](https://img.aspxhome.com/file/2023/4/97954_0s.webp)
解决Maven本地仓库明明有对应的jar包但还是报找不到的问题
![](https://img.aspxhome.com/file/2023/0/82010_0s.png)
使用Spring Expression Language (SpEL)全面解析表达式
![](https://img.aspxhome.com/file/2023/8/101998_0s.png)
java实现登录窗口
![](https://img.aspxhome.com/file/2023/1/110611_0s.png)
java中struts2实现简单的文件上传与下载
springboot快速整合Mybatis组件的方法(推荐)
![](https://img.aspxhome.com/file/2023/2/86052_0s.png)
springboot无法从静态上下文中引用非静态变量的解决方法
从java中调用matlab详细介绍
![](https://img.aspxhome.com/file/2023/0/59870_0s.jpg)
springBoot Junit测试用例出现@Autowired不生效的解决
![](https://img.aspxhome.com/file/2023/0/73120_0s.png)
Android ViewDragHelper使用介绍
![](https://img.aspxhome.com/file/2023/8/85458_0s.gif)
详解C++ STL模拟实现forward_list
![](https://img.aspxhome.com/file/2023/0/60100_0s.jpg)