如何从内置web浏览器而不是应用程序中的代码打开URL?

我试过了:

try {
    Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(download_link));
    startActivity(myIntent);
} catch (ActivityNotFoundException e) {
    Toast.makeText(this, "No application can handle this request."
        + " Please install a webbrowser",  Toast.LENGTH_LONG).show();
    e.printStackTrace();
}

但我有个例外:

No activity found to handle Intent{action=android.intent.action.VIEW data =www.google.com

当前回答

所以我找了很长时间,因为所有其他答案都是打开该链接的默认应用程序,而不是默认浏览器,这就是我想要的。

我终于做到了:

// gathering the default browser
final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"));
final ResolveInfo resolveInfo = context.getPackageManager()
    .resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
String defaultBrowserPackageName = resolveInfo.activityInfo.packageName;


final Intent intent2 = new Intent(Intent.ACTION_VIEW);
intent2.setData(Uri.parse(url));

if (!defaultBrowserPackageName.equals("android")) {
    // android = no default browser is set 
    // (android < 6 or fresh browser install or simply no default set)
    // if it's the case (not in this block), it will just use normal way.
    intent2.setPackage(defaultBrowserPackageName);
}

context.startActivity(intent2);

顺便说一句,你可以注意到context.whatever,因为我已经将它用于静态util方法,如果你在一个活动中这样做,它是不需要的。

其他回答

试试这个OmegaIntentBuilder

OmegaIntentBuilder.from(context)
                .web("Your url here")
                .createIntentHandler()
                .failToast("You don't have app for open urls")
                .startActivity();

你也可以走这条路

在xml中:

<?xml version="1.0" encoding="utf-8"?>
<WebView  
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

在java代码中:

public class WebViewActivity extends Activity {

private WebView webView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl("http://www.google.com");

 }

}

在清单中,不要忘记添加internet权限。。。

简单和最佳实践

方法1:

String intentUrl="www.google.com";
Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(intentUrl));
    if(webIntent.resolveActivity(getPackageManager())!=null){
        startActivity(webIntent);    
    }else{
      /*show Error Toast 
              or 
        Open play store to download browser*/
            }

方法2:

try{
    String intentUrl="www.google.com";
    Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(intentUrl));
        startActivity(webIntent);
    }catch (ActivityNotFoundException e){
                /*show Error Toast
                        or
                  Open play store to download browser*/
    }

简单的回答

你可以看到Android开发者的官方示例。

/**
 * Open a web page of a specified URL
 *
 * @param url URL to open
 */
public void openWebPage(String url) {
    Uri webpage = Uri.parse(url);
    Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }
}

它的工作原理

请查看Intent的构造函数:

public Intent (String action, Uri uri)

您可以将android.net.Uri实例传递给第二个参数,并根据给定的数据url创建一个新的Intent。

然后,只需调用startActivity(Intent Intent)来启动一个新的Activity,该Activity与具有给定URL的Intent绑定在一起。

我需要if check语句吗?

对医生说:

如果设备上没有可以接收到隐含意图的应用程序,那么当您的应用程序调用startActivity()时,它将崩溃。要首先验证应用程序是否存在以接收意图,请调用intent对象上的resolveActivity()。如果结果为非空,则至少有一个应用程序可以处理该意图,并且可以安全地调用startActivity()。如果结果为空,则不应使用意图,如果可能,应禁用调用意图的功能。

奖金

创建Intent实例时,可以在一行中编写如下内容:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

在try块中,粘贴以下代码,AndroidIntent直接使用URI(统一资源标识符)大括号中的链接来标识链接的位置。

你可以试试这个:

Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(myIntent);