Android开发_如何调用系统默认浏览器访问

http://blog.sina.com.cn/s/blog_6efce07e010142w7.html


一、启动android默认浏览器

 

    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)


                Intent intent= new Intent();        
                intent.setAction("android.intent.action.VIEW");    
                Uri content_url = Uri.parse("http://www.cnblogs.com");   
                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文件的时候,一定要指定某个浏览器,而不能采用方式一来浏览,具体示例代码如下

 

                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>后面就可以了

 

      <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“这个形式了


有兴趣的可以去试试。 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android开发中,将浏览器设置为默认浏览器需要以下三个步骤: 1. 在AndroidManifest.xml文件中添加如下代码: ``` <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="http" /> <data android:scheme="https" /> </intent-filter> </activity> ``` 上述代码中,我们设置了MainActivity作为处理在浏览器打开HTTP或HTTPS链接的Activity。这将把我们的应用程序添加到系统默认浏览器的列表中。 2. 在MainActivity.java文件中添加如下代码: ``` Uri uri = getIntent().getData(); Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri); startActivity(browserIntent); ``` 上述代码中,我们首先获取从浏览器传递的数据,并使用ACTION_VIEW Intent建立一个新的浏览器Intent,最后启动它。 3. 在res文件夹中的layout文件夹中创建一个布局文件,并添加一个使用onClick属性的按钮。在MainActivity.java文件中实现此按钮的点击事件并添加以下代码: ``` PackageManager pm = getPackageManager(); ComponentName componentName = new ComponentName(this, MainActivity.class); pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); Intent intent = new Intent(Intent.ACTION_VIEW); intent.addCategory(Intent.CATEGORY_BROWSABLE); intent.setData(Uri.parse("http://www.google.com")); startActivity(intent); ``` 上述代码中,我们使用PackageManager和ComponentName类启用我们的主Activity,设置intent的数据来打开特定的网站,并启动新的浏览器Intent。 通过上述三个步骤,我们可以将我们的应用设置为Android系统默认浏览器

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值