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

当前回答

简单的回答

你可以看到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开发人员可以使用

var webpage = Uri.parse(url)
    if (!url.startsWith("http://") && !url.startsWith("https://")) {
        webpage = Uri.parse("http://$url")
    }
    val intent = Intent(Intent.ACTION_VIEW, webpage)
    if (intent.resolveActivity(packageManager) != null) {
        startActivity(intent)
    }

科特林

startActivity(Intent(Intent.ACTION_VIEW).apply {
            data = Uri.parse(your_link)
        })

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
    }
}

Chrome自定义选项卡现在可用:

第一步是将自定义选项卡支持库添加到build.gradle文件中:

dependencies {
    ...
    compile 'com.android.support:customtabs:24.2.0'
}

然后,要打开chrome自定义选项卡:

String url = "https://www.google.pt/";
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));

有关详细信息:https://developer.chrome.com/multidevice/android/customtabs

试试这个OmegaIntentBuilder

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