先看一下系統浏覽器com.android.browser啟動類在AndroidManifest.xml中的聲明:
[xhtml] view plain copy
- <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">
- <intent-filter>
- <action android:name="android.speech.action.VOICE_SEARCH_RESULTS" />
- <category android:name="android.intent.category.DEFAULT" />
- </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="http" />
- <data android:scheme="https" />
- <data android:scheme="about" />
- <data android:scheme="javascript" />
- </intent-filter>
- <intent-filter>
- <action android:name="android.intent.action.VIEW" />
- <category android:name="android.intent.category.BROWSABLE" />
- <category android:name="android.intent.category.DEFAULT" />
- <data android:scheme="http" />
- <data android:scheme="https" />
- <data android:scheme="inline" />
- <data android:mimeType="text/html" />
- <data android:mimeType="text/plain" />
- <data android:mimeType="application/xhtml+xml" />
- <data android:mimeType="application/vnd.wap.xhtml+xml" />
- </intent-filter>
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.DEFAULT" />
- <category android:name="android.intent.category.LAUNCHER" />
- <category android:name="android.intent.category.BROWSABLE" />
- </intent-filter>
- <intent-filter>
- <action android:name="android.intent.action.WEB_SEARCH" />
- <category android:name="android.intent.category.DEFAULT" />
- <category android:name="android.intent.category.BROWSABLE" />
- <data android:scheme="" />
- <data android:scheme="http" />
- <data android:scheme="https" />
- </intent-filter>
- <intent-filter>
- <action android:name="android.intent.action.MEDIA_SEARCH" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- <intent-filter>
- <action android:name="android.intent.action.SEARCH" />
- <category android:name="android.intent.category.DEFAULT" />
- </intent-filter>
- <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
- </activity>
在action指派為”android.intent.action.VIEW“時可接收如下scheme為"http"等等類型的data。是以突發奇想,啟動該程式後,指定action及Uri,即通路指定網頁。好了,立馬動手實踐。代碼如下:
[java] view plain copy
- package lab.sodino.specifybrowser;
- import java.util.List;
- import android.app.Activity;
- import android.content.Intent;
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager;
- import android.net.Uri;
- import android.os.Bundle;
- public class VisitUrlAct extends Activity {
- private boolean letUserChoice = false;
- private String visitUrl = "http://blog.csdn.net/sodino";
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // showUCBrowser();
- // showQQBrowser();
- // showOperaBrowser();
- // showDolphinBrowser();
- // showSkyfireBrowser();
- // showSteelBrowser();
- if (letUserChoice) {
- doDefault();
- } else {
- choiceBrowserToVisitUrl(visitUrl);
- }
- // 直接退出程式
- finish();
- }
- private void choiceBrowserToVisitUrl(String url) {
- boolean existUC = false, existOpera = false, existQQ = false, existDolphin = false, existSkyfire = false, existSteel = false, existGoogle = false;
- String ucPath = "", operaPath = "", qqPath = "", dolphinPath = "", skyfirePath = "", steelPath = "", googlePath = "";
- PackageManager packageMgr = getPackageManager();
- List<PackageInfo> list = packageMgr.getInstalledPackages(0);
- for (int i = 0; i < list.size(); i++) {
- PackageInfo info = list.get(i);
- String temp = info.packageName;
- if (temp.equals("com.uc.browser")) {
- // 存在UC
- ucPath = temp;
- existUC = true;
- } else if (temp.equals("com.tencent.mtt")) {
- // 存在QQ
- qqPath = temp;
- existQQ = true;
- } else if (temp.equals("com.opera.mini.android")) {
- // 存在Opera
- operaPath = temp;
- existOpera = true;
- } else if (temp.equals("mobi.mgeek.TunnyBrowser")) {
- dolphinPath = temp;
- existDolphin = true;
- } else if (temp.equals("com.skyfire.browser")) {
- skyfirePath = temp;
- existSkyfire = true;
- } else if (temp.equals("com.kolbysoft.steel")) {
- steelPath = temp;
- existSteel = true;
- } else if (temp.equals("com.android.browser")) {
- // 存在GoogleBroser
- googlePath = temp;
- existGoogle = true;
- }
- }
- if (existUC) {
- gotoUrl(ucPath, url, packageMgr);
- } else if (existOpera) {
- gotoUrl(operaPath, url, packageMgr);
- } else if (existQQ) {
- gotoUrl(qqPath, url, packageMgr);
- } else if (existDolphin) {
- gotoUrl(dolphinPath, url, packageMgr);
- } else if (existSkyfire) {
- gotoUrl(skyfirePath, url, packageMgr);
- } else if (existSteel) {
- gotoUrl(steelPath, url, packageMgr);
- } else if (existGoogle) {
- gotoUrl(googlePath, url, packageMgr);
- } else {
- doDefault();
- }
- }
- private void gotoUrl(String packageName, String url,
- PackageManager packageMgr) {
- try {
- Intent intent;
- intent = packageMgr.getLaunchIntentForPackage(packageName);
- intent.setAction(Intent.ACTION_VIEW);
- intent.addCategory(Intent.CATEGORY_DEFAULT);
- intent.setData(Uri.parse(url));
- startActivity(intent);
- } catch (Exception e) {
- // 在1.5及以前版本會要求catch(android.content.pm.PackageManager.NameNotFoundException)異常,該異常在1.5以後版本已取消。
- e.printStackTrace();
- }
- }
- private void doDefault() {
- Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(visitUrl));
- startActivity(intent);
- }
- private void showUCBrowser() {
- Intent intent = new Intent();
- intent.setClassName("com.uc.browser", "com.uc.browser.ActivityUpdate");
- intent.setAction(Intent.ACTION_VIEW);
- intent.addCategory(Intent.CATEGORY_DEFAULT);
- intent.setData(Uri.parse(visitUrl));
- startActivity(intent);
- }
- private void showQQBrowser() {
- Intent intent = new Intent();
- intent.setClassName("com.tencent.mtt", "com.tencent.mtt.MainActivity");
- intent.setAction(Intent.ACTION_VIEW);
- intent.addCategory(Intent.CATEGORY_DEFAULT);
- intent.setData(Uri.parse(visitUrl));
- startActivity(intent);
- }
- private void showOperaBrowser() {
- Intent intent = new Intent();
- intent.setClassName("com.opera.mini.android",
- "com.opera.mini.android.Browser");
- intent.setAction(Intent.ACTION_VIEW);
- intent.addCategory(Intent.CATEGORY_DEFAULT);
- intent.setData(Uri.parse(visitUrl));
- startActivity(intent);
- }
- private void showDolphinBrowser() {
- // 方法一:
- // Intent intent = new Intent();
- // intent.setClassName("mobi.mgeek.TunnyBrowser",
- // "mobi.mgeek.TunnyBrowser.BrowserActivity");
- // intent.setAction(Intent.ACTION_VIEW);
- // intent.addCategory(Intent.CATEGORY_DEFAULT);
- // intent.setData(Uri.parse(visitUrl));
- // startActivity(intent);
- // 方法二:
- gotoUrl("mobi.mgeek.TunnyBrowser", visitUrl, getPackageManager());
- }
- private void showSkyfireBrowser() {
- // 方法一:
- Intent intent = new Intent();
- intent.setClassName("com.skyfire.browser",
- "com.skyfire.browser.core.Main");
- intent.setAction(Intent.ACTION_VIEW);
- intent.addCategory(Intent.CATEGORY_DEFAULT);
- intent.setData(Uri.parse(visitUrl));
- startActivity(intent);
- // 方法二:
- // gotoUrl("com.skyfire.browser", visitUrl, getPackageManager());
- }
- private void showSteelBrowser() {
- // 方法一:
- // Intent intent = new Intent();
- // intent.setClassName("com.kolbysoft.steel",
- // "com.kolbysoft.steel.Steel");
- // intent.setAction(Intent.ACTION_VIEW);
- // intent.addCategory(Intent.CATEGORY_DEFAULT);
- // intent.setData(Uri.parse(visitUrl));
- // startActivity(intent);
- // 方法二:
- gotoUrl("com.kolbysoft.steel", visitUrl, getPackageManager());
- }
- }
ok,成功了。
附choice.png圖檔: