[Android]指定浏览器访问指定页面(支持UC、Opera、QQ、Dolphin、Skyfire、Steel、Google)

先看一下系统浏览器com.android.browser启动类在AndroidManifest.xml中的声明:

[xhtml] view plain copy
  1. <activity android:theme="@style/BrowserTheme" android:label="@string/application_name" android:name="BrowserActivity" android:launchMode="singleTask" android:configChanges="keyboardHidden|orientation" android:alwaysRetainTaskState="true" android:windowSoftInputMode="adjustResize">  
  2.     <intent-filter>  
  3.         <action android:name="android.speech.action.VOICE_SEARCH_RESULTS" />  
  4.         <category android:name="android.intent.category.DEFAULT" />  
  5.     </intent-filter>  
  6.     <intent-filter>  
  7.         <action android:name="android.intent.action.VIEW" />  
  8.         <category android:name="android.intent.category.DEFAULT" />  
  9.         <category android:name="android.intent.category.BROWSABLE" />  
  10.         <data android:scheme="http" />  
  11.         <data android:scheme="https" />  
  12.         <data android:scheme="about" />  
  13.         <data android:scheme="javascript" />  
  14.     </intent-filter>  
  15.     <intent-filter>  
  16.         <action android:name="android.intent.action.VIEW" />  
  17.         <category android:name="android.intent.category.BROWSABLE" />  
  18.         <category android:name="android.intent.category.DEFAULT" />  
  19.         <data android:scheme="http" />  
  20.         <data android:scheme="https" />  
  21.         <data android:scheme="inline" />  
  22.         <data android:mimeType="text/html" />  
  23.         <data android:mimeType="text/plain" />  
  24.         <data android:mimeType="application/xhtml+xml" />  
  25.         <data android:mimeType="application/vnd.wap.xhtml+xml" />  
  26.     </intent-filter>  
  27.     <intent-filter>  
  28.         <action android:name="android.intent.action.MAIN" />  
  29.         <category android:name="android.intent.category.DEFAULT" />  
  30.         <category android:name="android.intent.category.LAUNCHER" />  
  31.         <category android:name="android.intent.category.BROWSABLE" />  
  32.     </intent-filter>  
  33.     <intent-filter>  
  34.         <action android:name="android.intent.action.WEB_SEARCH" />  
  35.         <category android:name="android.intent.category.DEFAULT" />  
  36.         <category android:name="android.intent.category.BROWSABLE" />  
  37.         <data android:scheme="" />  
  38.         <data android:scheme="http" />  
  39.         <data android:scheme="https" />  
  40.     </intent-filter>  
  41.     <intent-filter>  
  42.         <action android:name="android.intent.action.MEDIA_SEARCH" />  
  43.         <category android:name="android.intent.category.DEFAULT" />  
  44.     </intent-filter>  
  45.     <intent-filter>  
  46.         <action android:name="android.intent.action.SEARCH" />  
  47.         <category android:name="android.intent.category.DEFAULT" />  
  48.     </intent-filter>  
  49.     <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />  
  50. </activity>  

 

action赋值为”android.intent.action.VIEW“时可接收如下scheme"http"等等类型的data。所以突发奇想,启动该程序后,指定actionUri,即访问指定网页。好了,立马动手实践。代码如下:

 

  1. package lab.sodino.specifybrowser;  
  2. import java.util.List;  
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.content.pm.PackageInfo;  
  6. import android.content.pm.PackageManager;  
  7. import android.net.Uri;  
  8. import android.os.Bundle;  
  9. /** 
  10.  * @author Sodino 
  11.  * @version 2010年11月8日22时45分47秒 
  12.  * */  
  13. public class VisitUrlAct extends Activity {  
  14.     /** 
  15.      * 值为true时,使用传统方法让用户选择。<br/> 
  16.      * 值为false时,程序自动为用户选定浏览器浏览。<br/> 
  17.      * 目前支持且优先级从高到低为:<br/> 
  18.      * 1.UC浏览器<br/> 
  19.      * 2.Opera浏览器<br/> 
  20.      * 3.QQ浏览器<br/> 
  21.      * 4.Dolphin Browser(不支持WAP)<br/> 
  22.      * 5.Skyfire Browser(不支持WAP)<br/> 
  23.      * 6.Steel Browser(不支持WAP)<br/> 
  24.      * 7.系统浏览器<br/> 
  25.      * 验证是否支持直接启动访问指定页面的方法为:<br/> 
  26.      * 执行下面的<code>doDefault()</code>时会出现如下选择框,<br/> 
  27.      * 选择浏览器,如果能够正常访问,可以指定该浏览器访问指定网页;<br/> 
  28.      * 如果该浏览器启动后没有跳转到指定网页,则不支持。<br/> 
  29.      * 实践中,Go浏览器与天天浏览器并不支持。<br/> 
  30.      * <img src="../../../../choice.png" mce_src="choice.png"/> <br/> 
  31.      * 正确显示图片请在不改变包名的前提下将choice.png放于工程目录下,与src、res同级。<br/> 
  32.      */  
  33.     private boolean letUserChoice = false;  
  34.     private String visitUrl = "http://blog.csdn.net/sodino";  
  35.     public void onCreate(Bundle savedInstanceState) {  
  36.         super.onCreate(savedInstanceState);  
  37.         // showUCBrowser();  
  38.         // showQQBrowser();  
  39.         // showOperaBrowser();  
  40.         // showDolphinBrowser();  
  41.         // showSkyfireBrowser();  
  42.         // showSteelBrowser();  
  43.         if (letUserChoice) {  
  44.             doDefault();  
  45.         } else {  
  46.             choiceBrowserToVisitUrl(visitUrl);  
  47.         }  
  48.         // 直接退出程序  
  49.         finish();  
  50.     }  
  51.     private void choiceBrowserToVisitUrl(String url) {  
  52.         boolean existUC = false, existOpera = false, existQQ = false, existDolphin = false, existSkyfire = false, existSteel = false, existGoogle = false;  
  53.         String ucPath = "", operaPath = "", qqPath = "", dolphinPath = "", skyfirePath = "", steelPath = "", googlePath = "";  
  54.         PackageManager packageMgr = getPackageManager();  
  55.         List<PackageInfo> list = packageMgr.getInstalledPackages(0);  
  56.         for (int i = 0; i < list.size(); i++) {  
  57.             PackageInfo info = list.get(i);  
  58.             String temp = info.packageName;  
  59.             if (temp.equals("com.uc.browser")) {  
  60.                 // 存在UC  
  61.                 ucPath = temp;  
  62.                 existUC = true;  
  63.             } else if (temp.equals("com.tencent.mtt")) {  
  64.                 // 存在QQ  
  65.                 qqPath = temp;  
  66.                 existQQ = true;  
  67.             } else if (temp.equals("com.opera.mini.android")) {  
  68.                 // 存在Opera  
  69.                 operaPath = temp;  
  70.                 existOpera = true;  
  71.             } else if (temp.equals("mobi.mgeek.TunnyBrowser")) {  
  72.                 dolphinPath = temp;  
  73.                 existDolphin = true;  
  74.             } else if (temp.equals("com.skyfire.browser")) {  
  75.                 skyfirePath = temp;  
  76.                 existSkyfire = true;  
  77.             } else if (temp.equals("com.kolbysoft.steel")) {  
  78.                 steelPath = temp;  
  79.                 existSteel = true;  
  80.             } else if (temp.equals("com.android.browser")) {  
  81.                 // 存在GoogleBroser  
  82.                 googlePath = temp;  
  83.                 existGoogle = true;  
  84.             }  
  85.         }  
  86.         if (existUC) {  
  87.             gotoUrl(ucPath, url, packageMgr);  
  88.         } else if (existOpera) {  
  89.             gotoUrl(operaPath, url, packageMgr);  
  90.         } else if (existQQ) {  
  91.             gotoUrl(qqPath, url, packageMgr);  
  92.         } else if (existDolphin) {  
  93.             gotoUrl(dolphinPath, url, packageMgr);  
  94.         } else if (existSkyfire) {  
  95.             gotoUrl(skyfirePath, url, packageMgr);  
  96.         } else if (existSteel) {  
  97.             gotoUrl(steelPath, url, packageMgr);  
  98.         } else if (existGoogle) {  
  99.             gotoUrl(googlePath, url, packageMgr);  
  100.         } else {  
  101.             doDefault();  
  102.         }  
  103.     }  
  104.     private void gotoUrl(String packageName, String url,  
  105.             PackageManager packageMgr) {  
  106.         try {  
  107.             Intent intent;  
  108.             intent = packageMgr.getLaunchIntentForPackage(packageName);  
  109.             intent.setAction(Intent.ACTION_VIEW);  
  110.             intent.addCategory(Intent.CATEGORY_DEFAULT);  
  111.             intent.setData(Uri.parse(url));  
  112.             startActivity(intent);  
  113.         } catch (Exception e) {  
  114.             // 在1.5及以前版本会要求catch(android.content.pm.PackageManager.NameNotFoundException)异常,该异常在1.5以后版本已取消。  
  115.             e.printStackTrace();  
  116.         }  
  117.     }  
  118.     private void doDefault() {  
  119.         Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(visitUrl));  
  120.         startActivity(intent);  
  121.     }  
  122.     /** 直接启动UC,用于验证测试。 */  
  123.     private void showUCBrowser() {  
  124.         Intent intent = new Intent();  
  125.         intent.setClassName("com.uc.browser""com.uc.browser.ActivityUpdate");  
  126.         intent.setAction(Intent.ACTION_VIEW);  
  127.         intent.addCategory(Intent.CATEGORY_DEFAULT);  
  128.         intent.setData(Uri.parse(visitUrl));  
  129.         startActivity(intent);  
  130.     }  
  131.     /** 直接启动QQ,用于验证测试。 */  
  132.     private void showQQBrowser() {  
  133.         Intent intent = new Intent();  
  134.         intent.setClassName("com.tencent.mtt""com.tencent.mtt.MainActivity");  
  135.         intent.setAction(Intent.ACTION_VIEW);  
  136.         intent.addCategory(Intent.CATEGORY_DEFAULT);  
  137.         intent.setData(Uri.parse(visitUrl));  
  138.         startActivity(intent);  
  139.     }  
  140.     /** 直接启动Opera,用于验证测试。 */  
  141.     private void showOperaBrowser() {  
  142.         Intent intent = new Intent();  
  143.         intent.setClassName("com.opera.mini.android",  
  144.                 "com.opera.mini.android.Browser");  
  145.         intent.setAction(Intent.ACTION_VIEW);  
  146.         intent.addCategory(Intent.CATEGORY_DEFAULT);  
  147.         intent.setData(Uri.parse(visitUrl));  
  148.         startActivity(intent);  
  149.     }  
  150.     /** 直接启动Dolphin Browser,用于验证测试。 */  
  151.     private void showDolphinBrowser() {  
  152.         // 方法一:  
  153.         // Intent intent = new Intent();  
  154.         // intent.setClassName("mobi.mgeek.TunnyBrowser",  
  155.         // "mobi.mgeek.TunnyBrowser.BrowserActivity");  
  156.         // intent.setAction(Intent.ACTION_VIEW);  
  157.         // intent.addCategory(Intent.CATEGORY_DEFAULT);  
  158.         // intent.setData(Uri.parse(visitUrl));  
  159.         // startActivity(intent);  
  160.         // 方法二:  
  161.         gotoUrl("mobi.mgeek.TunnyBrowser", visitUrl, getPackageManager());  
  162.     }  
  163.     /** 直接启动Skyfire Browser,用于验证测试。 */  
  164.     private void showSkyfireBrowser() {  
  165.         // 方法一:  
  166.         Intent intent = new Intent();  
  167.         intent.setClassName("com.skyfire.browser",  
  168.                 "com.skyfire.browser.core.Main");  
  169.         intent.setAction(Intent.ACTION_VIEW);  
  170.         intent.addCategory(Intent.CATEGORY_DEFAULT);  
  171.         intent.setData(Uri.parse(visitUrl));  
  172.         startActivity(intent);  
  173.         // 方法二:  
  174.         // gotoUrl("com.skyfire.browser", visitUrl, getPackageManager());  
  175.     }  
  176.     /** 直接启动Steel Browser,用于验证测试。 */  
  177.     private void showSteelBrowser() {  
  178.         // 方法一:  
  179.         // Intent intent = new Intent();  
  180.         // intent.setClassName("com.kolbysoft.steel",  
  181.         // "com.kolbysoft.steel.Steel");  
  182.         // intent.setAction(Intent.ACTION_VIEW);  
  183.         // intent.addCategory(Intent.CATEGORY_DEFAULT);  
  184.         // intent.setData(Uri.parse(visitUrl));  
  185.         // startActivity(intent);  
  186.         // 方法二:  
  187.         gotoUrl("com.kolbysoft.steel", visitUrl, getPackageManager());  
  188.     }  
  189. }  

 

 

ok,成功了。

 

附choice.png图片:

choice


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值