先看一下系统浏览器com.android.browser 启动类在AndroidManifest.xml 中的声明:
01
<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">
02
<intent-filter>
03
<action android:name="android.speech.action.VOICE_SEARCH_RESULTS" />
04
<category android:name="android.intent.category.DEFAULT" />
05
</intent-filter>
06
<intent-filter>
07
<action android:name="android.intent.action.VIEW" />
08
<category android:name="android.intent.category.DEFAULT" />
09
<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 ,即访问指定网页。好了,立马动手实践。代码如下:
001
package lab.sodino.specifybrowser;
002
003
import java.util.List;
004
import android.app.Activity;
005
import android.content.Intent;
006
import android.content.pm.PackageInfo;
007
import android.content.pm.PackageManager;
008
import android.net.Uri;
009
import android.os.Bundle;
010
011
/**
012
* @author Sodino
013
* @version 2010年11月8日22时45分47秒
014
* */
015
public class VisitUrlAct extends Activity {
016
/**
017
* 值为true时,使用传统方法让用户选择。<br/>
018
* 值为false时,程序自动为用户选定浏览器浏览。<br/>
019
* 目前支持且优先级从高到低为:<br/>
020
* 1.UC浏览器<br/>
021
* 2.Opera浏览器<br/>
022
* 3.QQ浏览器<br/>
023
* 4.Dolphin Browser(不支持WAP)<br/>
024
* 5.Skyfire Browser(不支持WAP)<br/>
025
* 6.Steel Browser(不支持WAP)<br/>
026
* 7.系统浏览器<br/>
027
* 验证是否支持直接启动访问指定页面的方法为:<br/>
028
* 执行下面的<code>doDefault()</code>时会出现如下选择框,<br/>
029
* 选择浏览器,如果能够正常访问,可以指定该浏览器访问指定网页;<br/>
030
* 如果该浏览器启动后没有跳转到指定网页,则不支持。<br/>
031
* 实践中,Go浏览器与天天浏览器并不支持。<br/>
032
* <img src="../../../../choice.png" mce_src="choice.png"/> <br/>
033
* 正确显示图片请在不改变包名的前提下将choice.png放于工程目录下,与src、res同级。<br/>
034
*/
035
private boolean letUserChoice = false;
036
private String visitUrl = "http://blog.csdn.net/sodino";
037
public void onCreate(Bundle savedInstanceState) {
038
super.onCreate(savedInstanceState);
039
// showUCBrowser();
040
// showQQBrowser();
041
// showOperaBrowser();
042
// showDolphinBrowser();
043
// showSkyfireBrowser();
044
// showSteelBrowser();
045
if (letUserChoice) {
046
doDefault();
047
} else {
048
choiceBrowserToVisitUrl(visitUrl);
049
}
050
// 直接退出程序
051
finish();
052
}
053
private void choiceBrowserToVisitUrl(String url) {
054
boolean existUC = false, existOpera = false, existQQ = false, existDolphin = false, existSkyfire = false, existSteel = false, existGoogle =false;
055
String ucPath = "", operaPath = "", qqPath = "", dolphinPath = "", skyfirePath = "", steelPath = "", googlePath = "";
056
PackageManager packageMgr = getPackageManager();
057
List<PackageInfo> list = packageMgr.getInstalledPackages(0);
058
for (int i = 0; i < list.size(); i++) {
059
PackageInfo info = list.get(i);
060
String temp = info.packageName;
061
if (temp.equals("com.uc.browser")) {
062
// 存在UC
063
ucPath = temp;
064
existUC = true;
065
} else if (temp.equals("com.tencent.mtt")) {
066
// 存在QQ
067
qqPath = temp;
068
existQQ = true;
069
} else if (temp.equals("com.opera.mini.android")) {
070
// 存在Opera
071
operaPath = temp;
072
existOpera = true;
073
} else if (temp.equals("mobi.mgeek.TunnyBrowser")) {
074
dolphinPath = temp;
075
existDolphin = true;
076
} else if (temp.equals("com.skyfire.browser")) {
077
skyfirePath = temp;
078
existSkyfire = true;
079
} else if (temp.equals("com.kolbysoft.steel")) {
080
steelPath = temp;
081
existSteel = true;
082
} else if (temp.equals("com.android.browser")) {
083
// 存在GoogleBroser
084
googlePath = temp;
085
existGoogle = true;
086
}
087
}
088
if (existUC) {
089
gotoUrl(ucPath, url, packageMgr);
090
} else if (existOpera) {
091
gotoUrl(operaPath, url, packageMgr);
092
} else if (existQQ) {
093
gotoUrl(qqPath, url, packageMgr);
094
} else if (existDolphin) {
095
gotoUrl(dolphinPath, url, packageMgr);
096
} else if (existSkyfire) {
097
gotoUrl(skyfirePath, url, packageMgr);
098
} else if (existSteel) {
099
gotoUrl(steelPath, url, packageMgr);
100
} else if (existGoogle) {
101
gotoUrl(googlePath, url, packageMgr);
102
} else {
103
doDefault();
104
}
105
}
106
private void gotoUrl(String packageName, String url,
107
PackageManager packageMgr) {
108
try {
109
Intent intent;
110
intent = packageMgr.getLaunchIntentForPackage(packageName);
111
intent.setAction(Intent.ACTION_VIEW);
112
intent.addCategory(Intent.CATEGORY_DEFAULT);
113
intent.setData(Uri.parse(url));
114
startActivity(intent);
115
} catch (Exception e) {
116
// 在1.5及以前版本会要求catch(android.content.pm.PackageManager.NameNotFoundException)异常,该异常在1.5以后版本已取消。
117
e.printStackTrace();
118
}
119
}
120
private void doDefault() {
121
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(visitUrl));
122
startActivity(intent);
123
}
124
/** 直接启动UC,用于验证测试。 */
125
private void showUCBrowser() {
126
Intent intent = new Intent();
127
intent.setClassName("com.uc.browser", "com.uc.browser.ActivityUpdate");
128
intent.setAction(Intent.ACTION_VIEW);
129
intent.addCategory(Intent.CATEGORY_DEFAULT);
130
intent.setData(Uri.parse(visitUrl));
131
startActivity(intent);
132
}
133
/** 直接启动QQ,用于验证测试。 */
134
private void showQQBrowser() {
135
Intent intent = new Intent();
136
intent.setClassName("com.tencent.mtt", "com.tencent.mtt.MainActivity");
137
intent.setAction(Intent.ACTION_VIEW);
138
intent.addCategory(Intent.CATEGORY_DEFAULT);
139
intent.setData(Uri.parse(visitUrl));
140
startActivity(intent);
141
}
142
/** 直接启动Opera,用于验证测试。 */
143
private void showOperaBrowser() {
144
Intent intent = new Intent();
145
intent.setClassName("com.opera.mini.android",
146
"com.opera.mini.android.Browser");
147
intent.setAction(Intent.ACTION_VIEW);
148
intent.addCategory(Intent.CATEGORY_DEFAULT);
149
intent.setData(Uri.parse(visitUrl));
150
startActivity(intent);
151
}
152
/** 直接启动Dolphin Browser,用于验证测试。 */
153
private void showDolphinBrowser() {
154
// 方法一:
155
// Intent intent = new Intent();
156
// intent.setClassName("mobi.mgeek.TunnyBrowser",
157
// "mobi.mgeek.TunnyBrowser.BrowserActivity");
158
// intent.setAction(Intent.ACTION_VIEW);
159
// intent.addCategory(Intent.CATEGORY_DEFAULT);
160
// intent.setData(Uri.parse(visitUrl));
161
// startActivity(intent);
162
// 方法二:
163
gotoUrl("mobi.mgeek.TunnyBrowser", visitUrl, getPackageManager());
164
}
165
/** 直接启动Skyfire Browser,用于验证测试。 */
166
private void showSkyfireBrowser() {
167
// 方法一:
168
Intent intent = new Intent();
169
intent.setClassName("com.skyfire.browser",
170
"com.skyfire.browser.core.Main");
171
intent.setAction(Intent.ACTION_VIEW);
172
intent.addCategory(Intent.CATEGORY_DEFAULT);
173
intent.setData(Uri.parse(visitUrl));
174
startActivity(intent);
175
// 方法二:
176
// gotoUrl("com.skyfire.browser", visitUrl, getPackageManager());
177
}
178
/** 直接启动Steel Browser,用于验证测试。 */
179
private void showSteelBrowser() {
180
// 方法一:
181
// Intent intent = new Intent();
182
// intent.setClassName("com.kolbysoft.steel",
183
// "com.kolbysoft.steel.Steel");
184
// intent.setAction(Intent.ACTION_VIEW);
185
// intent.addCategory(Intent.CATEGORY_DEFAULT);
186
// intent.setData(Uri.parse(visitUrl));
187
// startActivity(intent);
188
// 方法二:
189
gotoUrl("com.kolbysoft.steel", visitUrl, getPackageManager());
190
}
191
}
ok,成功了。
附choice.png图片: