先看一下系统微端com.android.browser启动类在AndroidManifest.xml中的声明:
- <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,即访问指定网页。好了,立马动手实践。代码如下:
- 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 = "";
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
-
-
-
-
-
-
- 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")) {
-
- ucPath = temp;
- existUC = true;
- } else if (temp.equals("com.tencent.mtt")) {
-
- qqPath = temp;
- existQQ = true;
- } else if (temp.equals("com.opera.mini.android")) {
-
- 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")) {
-
- 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) {
-
- 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() {
-
-
-
-
-
-
-
-
-
- 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);
-
-
- }
-
- private void showSteelBrowser() {
-
-
-
-
-
-
-
-
-
- gotoUrl("com.kolbysoft.steel", visitUrl, getPackageManager());
- }
- }
ok,成功了。
附choice.png图片:
|
|