我有一部安装了多个浏览器的安卓手机,我可能会也可能不会将浏览器设置为默认值。
所以,我的问题是..
- 从我的应用程序中,如何强制仅在NATIVE android 浏览器中打开链接?
- 有没有办法知道浏览器是否设置为默认?
我有一部安装了多个浏览器的安卓手机,我可能会也可能不会将浏览器设置为默认值。
所以,我的问题是..
从我的应用程序中,如何强制仅在 NATIVE android 浏览器中打开链接?
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
}
}
终于想通了。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");
}
}