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

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

其他回答

如果您想向用户显示一个带有所有浏览器列表的对话框,以便他可以选择首选项,下面是示例代码:

private static final String HTTPS = "https://";
private static final String HTTP = "http://";

public static void openBrowser(final Context context, String url) {

     if (!url.startsWith(HTTP) && !url.startsWith(HTTPS)) {
            url = HTTP + url;
     }

     Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
     context.startActivity(Intent.createChooser(intent, "Choose browser"));// Choose browser is arbitrary :)

}
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
dataWebView.setWebViewClient(new VbLinksWebClient() {
     @Override
     public void onPageFinished(WebView webView, String url) {
           super.onPageFinished(webView, url);
     }
});




public class VbLinksWebClient extends WebViewClient
{
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url)
    {
        view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url.trim())));
        return true;
    }
}

根据Mark B的回答和以下评论:

protected void launchUrl(String url) {
    Uri uri = Uri.parse(url);

    if (uri.getScheme() == null || uri.getScheme().isEmpty()) {
        uri = Uri.parse("http://" + url);
    }

    Intent browserIntent = new Intent(Intent.ACTION_VIEW, uri);

    if (browserIntent.resolveActivity(getPackageManager()) != null) {
        startActivity(browserIntent);
    }
}

基本介绍:

https://正在“代码”中使用该代码,这样中间的任何人都无法读取它们。这样可以保护您的信息免受黑客攻击。

http://仅用于共享目的,不安全。

关于您的问题:XML设计:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.sridhar.sharedpreferencesstackoverflow.MainActivity">
   <LinearLayout
       android:orientation="horizontal"
       android:background="#228b22"
       android:layout_weight="1"
       android:layout_width="match_parent"
       android:layout_height="0dp">
      <Button
          android:id="@+id/normal_search"
          android:text="secure Search"
          android:onClick="secure"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="wrap_content" />
      <Button
          android:id="@+id/secure_search"
          android:text="Normal Search"
          android:onClick="normal"
          android:layout_weight="1"
          android:layout_width="0dp"
          android:layout_height="wrap_content" />
   </LinearLayout>

   <LinearLayout
       android:layout_weight="9"
       android:id="@+id/button_container"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:orientation="horizontal">

      <WebView
          android:id="@+id/webView1"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />

   </LinearLayout>
</LinearLayout>

活动设计:

public class MainActivity extends Activity {
    //securely open the browser
    public String Url_secure="https://www.stackoverflow.com";
    //normal purpouse
    public String Url_normal="https://www.stackoverflow.com";

    WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView=(WebView)findViewById(R.id.webView1);

    }
    public void secure(View view){
        webView.setWebViewClient(new SecureSearch());
        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webView.loadUrl(Url_secure);
    }
    public void normal(View view){
        webView.setWebViewClient(new NormalSearch());
        webView.getSettings().setLoadsImagesAutomatically(true);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        webView.loadUrl(Url_normal);

    }
    public class SecureSearch extends WebViewClient{
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String Url_secure) {
            view.loadUrl(Url_secure);
            return true;
        }
    }
    public class NormalSearch extends WebViewClient{
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String Url_normal) {
            view.loadUrl(Url_normal);
            return true;
        }
    }
}

Android Manifest.Xml权限:

<uses-permission android:name="android.permission.INTERNET"/>

您在执行此操作时面临问题:

获取清单权限url之间的多余空间检查您的url是否正确