我有一个安卓手机,上面装有多个浏览器,我可能设置了一个默认浏览器,也可能没有。
因此,我的问题是...
- 从我的应用程序中,如何强制在 本地 安卓浏览器中打开链接?
- 是否有一种方法可以知道是否设置了默认浏览器?
我有一个安卓手机,上面装有多个浏览器,我可能设置了一个默认浏览器,也可能没有。
因此,我的问题是...
Intent intent = new Intent();
ComponentName comp = new ComponentName("com.google.android.browser","com.google.android.browser.BrowserActivity");
intent.setComponent(comp);
intent.setAction("android.intent.action.VIEW");
intent.addCategory("android.intent.category.BROWSABLE");
Uri uri = Uri.parse(url);
intent.setData(uri);
try
{
startActivity(intent);
}
catch (Exception e)
{
e.printStackTrace();
}
有没有办法知道浏览器是否设置为默认浏览器?
PackageManager packageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("URL"));
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, 0);
if (list.size() > 0) {
for (ResolveInfo resolveInfo : list) {
resolveInfo.isDefault();// will let u know if the app is set to default
}
} else {
//No apps available
}
intent.setComponent(new
componentName("com.android.browser","com.android.browser.BrowserActivity"));
尝试这样做。
try {
Intent i = new Intent();
ComponentName comp = new ComponentName("com.google.android.browser","com.google.android.browser.BrowserActivity");
i.setComponent(comp);
i.setAction("android.intent.action.VIEW");
i.addCategory("android.intent.category.BROWSABLE");
ContentURI uri = new ContentURI(url);
i.setData(uri);
startActivityForResult(i, 2);
} catch (URISyntaxException e) {
e.printStackTrace();
}
对于你的第二个问题,你可以使用PackageManager
。
获取PackageManager
实例。
PackageManager packageManager = getPackageManager();
并查询特定的操作、数据和Intent
类别。
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("URL"));
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, 0);
for (ResolveInfo resolveInfo : list) {
if(resolveInfo.isDefault())
{
//default browser
}
}
Intent ii = new Intent();
ComponentName comp = new ComponentName("com.android.browser","com.android.browser.BrowserActivity");
ii.setComponent(comp);
ii.setAction("android.intent.action.VIEW");
ii.addCategory("android.intent.category.BROWSABLE");
ii.setData(Uri.parse("http://www.google.com"));
startActivity(ii);
对于第二个问题,是否有办法知道手机上是否设置了默认浏览器? - defactodeityresolveInfo.isDefault
似乎无法正常工作。 - defactodeity终于搞定了。 resolveActivity与PackageManager实例上的MATCH_DEFAULT_ONLY标志一起使用。
如果制造商的软件包名称不同,可能会发生ActivityNotFoundException。请参考此答案,希望能够帮助。
请检查这段代码:
private final static List<ComponentName> browserComponents = new ArrayList<ComponentName>() {{
add(new ComponentName("com.google.android.browser", "com.google.android.browser.BrowserActivity"));
add(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
add(new ComponentName("com.android.chrome", "com.google.android.apps.chrome.IntentDispatcher"));
}};
public static void openInNativeBrowser(Context context, String url) {
if (context == null || TextUtils.isEmpty(url)) {
return;
}
PackageManager pm = context.getPackageManager();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse(url));
for (ComponentName cn : browserComponents) {
intent.setComponent(cn);
ActivityInfo ai = intent.resolveActivityInfo(pm, 0);
if (ai != null) {
aLog.w(TAG, "browser: " + ai);
context.startActivity(intent);
return;
} else {
aLog.w(TAG, "can't resolve activity for " + intent);
}
}
// no native browser
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
List<ResolveInfo> list = pm.queryIntentActivities(intent, 0);
if (list.size() > 0) {
for (ResolveInfo ri : list) {
aLog.w(TAG, ri + " : " + ri.isDefault);
}
context.startActivity(intent);
} else {
aLog.w(TAG, "no browser apps");
}
}
resolveInfo.isDefault
从未对任何浏览器返回true!请帮忙。 - defactodeity