如何从内置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

当前回答

简短甜美的Kotlin助手功能:

private fun openUrl(link: String) =
    startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(link)))

其他回答

kolin扩展函数。

 fun Activity.openWebPage(url: String?) = url?.let {
    val intent = Intent(Intent.ACTION_VIEW, Uri.parse(it))
    if (intent.resolveActivity(packageManager) != null) startActivity(intent)
}

简单和最佳实践

方法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));

Kotlin回答:

val browserIntent = Intent(Intent.ACTION_VIEW, uri)
ContextCompat.startActivity(context, browserIntent, null)

我在Uri上添加了一个扩展,以使这更加容易

myUri.openInBrowser(context)

fun Uri?.openInBrowser(context: Context) {
    this ?: return // Do nothing if uri is null

    val browserIntent = Intent(Intent.ACTION_VIEW, this)
    ContextCompat.startActivity(context, browserIntent, null)
}

另外,这里有一个简单的扩展函数,可以将字符串安全地转换为Uri。

"https://stackoverflow.com".asUri()?.openInBrowser(context)

fun String?.asUri(): Uri? {
    return try {
        Uri.parse(this)
    } catch (e: Exception) {
        null
    }
}

//OnClick侦听器

  @Override
      public void onClick(View v) {
        String webUrl = news.getNewsURL();
        if(webUrl!="")
        Utils.intentWebURL(mContext, webUrl);
      }

//你的Util方法

public static void intentWebURL(Context context, String url) {
        if (!url.startsWith("http://") && !url.startsWith("https://")) {
            url = "http://" + url;
        }
        boolean flag = isURL(url);
        if (flag) {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW,
                    Uri.parse(url));
            context.startActivity(browserIntent);
        }

    }