从Android应用程序打开本机浏览器

def*_*ity 8 browser android mobile-browser android-intent

我有一个安装了多个浏览器的Android手机,我可能会也可能不会将浏览器设置为默认值.

所以,我的问题是......

  1. 从我的应用程序,如何强制只在NATIVE android浏览器中打开一个链接?
  2. 有没有办法让我知道浏览器是否设置为默认值?

gra*_*ito 7

从我的应用程序,如何强制只在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();
 } 
Run Code Online (Sandbox Code Playgroud)

有没有办法让我知道浏览器是否设置为默认值?

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
}
Run Code Online (Sandbox Code Playgroud)


Pra*_*rge 5

您必须进行以下操作才能调用本机浏览器

  intent.setComponent(new    
  componentName("com.android.browser","com.android.browser.BrowserActivity"));
Run Code Online (Sandbox Code Playgroud)