Android打算打开用户首选的浏览器

问题描述 投票:13回答:6

我一直在试图找出如何创建一个意图,打开用户首选的浏览器而不指定URL。我知道如何通过提供这样的特定URL来打开它:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(android.net.Uri.parse("http://www.google.com"));
context.startActivity(intent);

我不想打开浏览器到任何页面,特别是设置主页或用户最后的页面。我已经考虑过在应用程序中查找主页集,但是你无法使用默认的浏览器应用程序,因为它是私有的。有人知道这样做的方法吗?

android browser android-intent
6个回答
9
投票

我是这样做的:

String packageName = "com.android.browser"; 
String className = "com.android.browser.BrowserActivity"; 
Intent internetIntent = new Intent(Intent.ACTION_VIEW);
internetIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
internetIntent.setClassName(packageName, className); 
mHomeActivity.startActivity(internetIntent);

如果您没有设置主页,它将打开一个空白页面(至少在Android 2.1中)。


5
投票
            Uri uri = Uri.parse("www.google.com");
            Intent intent = new Intent(Intent.ACTION_VIEW,uri);
            // Create and start the chooser
            Intent chooser = Intent.createChooser(intent, "Open with");
            startActivity(chooser);

此代码创建打开用户指定浏览器的意图。


2
投票

这是一个迟到的答案,但看起来这个功能在API 15中可用:

  Intent browser = Intent.makeMainSelectorActivity(
         Intent.ACTION_VIEW, 
         Intent.CATEGORY_APP_BROWSER);

  startActivity(browser);

Docs for makeMainSelectorActivity


1
投票

用户指定的主页URL将存储在他们正在使用的任何浏览器应用的首选项中。使用Androids的“沙盒”应用程序模型,除非应用程序具有允许访问的内容提供程序,否则您将无权访问此应用程序。此外,内容提供商在浏览器应用程序之间会有所不同,您将难以涵盖确实存在的应用程序。

您是否尝试过打开试图通过使用JavaScript更新用户主页URL的网页?


1
投票

试试这个:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_BROWSER);
if (intent.resolveActivity(getPackageManager()) != null) {
    startActivity(intent.createChooser(intent, "Select Browser"));
}
else { //Popup a msg so as to know the reason of not opening the browser
    Toast.makeText(this, "There is no Browser App", Toast.LENGTH_LONG).show();
}

为我工作,也希望你!


0
投票

使用以下代码

Intent sendIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.stackoverflow.com"));
Intent chooser = Intent.createChooser(sendIntent, "Choose Your Browser");
if (sendIntent.resolveActivity(getPackageManager()) != null) 
    startActivity(chooser);
© www.soinside.com 2019 - 2024. All rights reserved.