天天看點

[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。是以突發奇想,啟動該程式後,指定action及Uri,即通路指定網頁。好了,立馬動手實踐。代碼如下:

[java] view plain copy

  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. public class VisitUrlAct extends Activity {  
  10.     private boolean letUserChoice = false;  
  11.     private String visitUrl = "http://blog.csdn.net/sodino";  
  12.     public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         // showUCBrowser();  
  15.         // showQQBrowser();  
  16.         // showOperaBrowser();  
  17.         // showDolphinBrowser();  
  18.         // showSkyfireBrowser();  
  19.         // showSteelBrowser();  
  20.         if (letUserChoice) {  
  21.             doDefault();  
  22.         } else {  
  23.             choiceBrowserToVisitUrl(visitUrl);  
  24.         }  
  25.         // 直接退出程式  
  26.         finish();  
  27.     }  
  28.     private void choiceBrowserToVisitUrl(String url) {  
  29.         boolean existUC = false, existOpera = false, existQQ = false, existDolphin = false, existSkyfire = false, existSteel = false, existGoogle = false;  
  30.         String ucPath = "", operaPath = "", qqPath = "", dolphinPath = "", skyfirePath = "", steelPath = "", googlePath = "";  
  31.         PackageManager packageMgr = getPackageManager();  
  32.         List<PackageInfo> list = packageMgr.getInstalledPackages(0);  
  33.         for (int i = 0; i < list.size(); i++) {  
  34.             PackageInfo info = list.get(i);  
  35.             String temp = info.packageName;  
  36.             if (temp.equals("com.uc.browser")) {  
  37.                 // 存在UC  
  38.                 ucPath = temp;  
  39.                 existUC = true;  
  40.             } else if (temp.equals("com.tencent.mtt")) {  
  41.                 // 存在QQ  
  42.                 qqPath = temp;  
  43.                 existQQ = true;  
  44.             } else if (temp.equals("com.opera.mini.android")) {  
  45.                 // 存在Opera  
  46.                 operaPath = temp;  
  47.                 existOpera = true;  
  48.             } else if (temp.equals("mobi.mgeek.TunnyBrowser")) {  
  49.                 dolphinPath = temp;  
  50.                 existDolphin = true;  
  51.             } else if (temp.equals("com.skyfire.browser")) {  
  52.                 skyfirePath = temp;  
  53.                 existSkyfire = true;  
  54.             } else if (temp.equals("com.kolbysoft.steel")) {  
  55.                 steelPath = temp;  
  56.                 existSteel = true;  
  57.             } else if (temp.equals("com.android.browser")) {  
  58.                 // 存在GoogleBroser  
  59.                 googlePath = temp;  
  60.                 existGoogle = true;  
  61.             }  
  62.         }  
  63.         if (existUC) {  
  64.             gotoUrl(ucPath, url, packageMgr);  
  65.         } else if (existOpera) {  
  66.             gotoUrl(operaPath, url, packageMgr);  
  67.         } else if (existQQ) {  
  68.             gotoUrl(qqPath, url, packageMgr);  
  69.         } else if (existDolphin) {  
  70.             gotoUrl(dolphinPath, url, packageMgr);  
  71.         } else if (existSkyfire) {  
  72.             gotoUrl(skyfirePath, url, packageMgr);  
  73.         } else if (existSteel) {  
  74.             gotoUrl(steelPath, url, packageMgr);  
  75.         } else if (existGoogle) {  
  76.             gotoUrl(googlePath, url, packageMgr);  
  77.         } else {  
  78.             doDefault();  
  79.         }  
  80.     }  
  81.     private void gotoUrl(String packageName, String url,  
  82.             PackageManager packageMgr) {  
  83.         try {  
  84.             Intent intent;  
  85.             intent = packageMgr.getLaunchIntentForPackage(packageName);  
  86.             intent.setAction(Intent.ACTION_VIEW);  
  87.             intent.addCategory(Intent.CATEGORY_DEFAULT);  
  88.             intent.setData(Uri.parse(url));  
  89.             startActivity(intent);  
  90.         } catch (Exception e) {  
  91.             // 在1.5及以前版本會要求catch(android.content.pm.PackageManager.NameNotFoundException)異常,該異常在1.5以後版本已取消。  
  92.             e.printStackTrace();  
  93.         }  
  94.     }  
  95.     private void doDefault() {  
  96.         Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(visitUrl));  
  97.         startActivity(intent);  
  98.     }  
  99.     private void showUCBrowser() {  
  100.         Intent intent = new Intent();  
  101.         intent.setClassName("com.uc.browser", "com.uc.browser.ActivityUpdate");  
  102.         intent.setAction(Intent.ACTION_VIEW);  
  103.         intent.addCategory(Intent.CATEGORY_DEFAULT);  
  104.         intent.setData(Uri.parse(visitUrl));  
  105.         startActivity(intent);  
  106.     }  
  107.     private void showQQBrowser() {  
  108.         Intent intent = new Intent();  
  109.         intent.setClassName("com.tencent.mtt", "com.tencent.mtt.MainActivity");  
  110.         intent.setAction(Intent.ACTION_VIEW);  
  111.         intent.addCategory(Intent.CATEGORY_DEFAULT);  
  112.         intent.setData(Uri.parse(visitUrl));  
  113.         startActivity(intent);  
  114.     }  
  115.     private void showOperaBrowser() {  
  116.         Intent intent = new Intent();  
  117.         intent.setClassName("com.opera.mini.android",  
  118.                 "com.opera.mini.android.Browser");  
  119.         intent.setAction(Intent.ACTION_VIEW);  
  120.         intent.addCategory(Intent.CATEGORY_DEFAULT);  
  121.         intent.setData(Uri.parse(visitUrl));  
  122.         startActivity(intent);  
  123.     }  
  124.     private void showDolphinBrowser() {  
  125.         // 方法一:  
  126.         // Intent intent = new Intent();  
  127.         // intent.setClassName("mobi.mgeek.TunnyBrowser",  
  128.         // "mobi.mgeek.TunnyBrowser.BrowserActivity");  
  129.         // intent.setAction(Intent.ACTION_VIEW);  
  130.         // intent.addCategory(Intent.CATEGORY_DEFAULT);  
  131.         // intent.setData(Uri.parse(visitUrl));  
  132.         // startActivity(intent);  
  133.         // 方法二:  
  134.         gotoUrl("mobi.mgeek.TunnyBrowser", visitUrl, getPackageManager());  
  135.     }  
  136.     private void showSkyfireBrowser() {  
  137.         // 方法一:  
  138.         Intent intent = new Intent();  
  139.         intent.setClassName("com.skyfire.browser",  
  140.                 "com.skyfire.browser.core.Main");  
  141.         intent.setAction(Intent.ACTION_VIEW);  
  142.         intent.addCategory(Intent.CATEGORY_DEFAULT);  
  143.         intent.setData(Uri.parse(visitUrl));  
  144.         startActivity(intent);  
  145.         // 方法二:  
  146.         // gotoUrl("com.skyfire.browser", visitUrl, getPackageManager());  
  147.     }  
  148.     private void showSteelBrowser() {  
  149.         // 方法一:  
  150.         // Intent intent = new Intent();  
  151.         // intent.setClassName("com.kolbysoft.steel",  
  152.         // "com.kolbysoft.steel.Steel");  
  153.         // intent.setAction(Intent.ACTION_VIEW);  
  154.         // intent.addCategory(Intent.CATEGORY_DEFAULT);  
  155.         // intent.setData(Uri.parse(visitUrl));  
  156.         // startActivity(intent);  
  157.         // 方法二:  
  158.         gotoUrl("com.kolbysoft.steel", visitUrl, getPackageManager());  
  159.     }  
  160. }  

ok,成功了。

附choice.png圖檔:

[Android]指定浏覽器通路指定頁面(支援UC、Opera、QQ、Dolphin、Skyfire、Steel、Google)