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

当前回答

这种方式使用一种方法,允许您输入任何字符串,而不是固定输入。如果重复使用多次,这确实会节省一些代码行,因为只需要三行代码就可以调用该方法。

public Intent getWebIntent(String url) {
    //Make sure it is a valid URL before parsing the URL.
    if(!url.contains("http://") && !url.contains("https://")){
        //If it isn't, just add the HTTP protocol at the start of the URL.
        url = "http://" + url;
    }
    //create the intent
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)/*And parse the valid URL. It doesn't need to be changed at this point, it we don't create an instance for it*/);
    if (intent.resolveActivity(getPackageManager()) != null) {
        //Make sure there is an app to handle this intent
        return intent;
    }
    //If there is no app, return null.
    return null;
}

使用此方法使其通用。IT不必放在特定的活动中,因为您可以这样使用它:

Intent i = getWebIntent("google.com");
if(i != null)
    startActivity();

或者,如果您想在活动外部启动它,只需在活动实例上调用startActivity:

Intent i = getWebIntent("google.com");
if(i != null)
    activityInstance.startActivity(i);

正如在这两个代码块中看到的,存在空检查。这是因为如果没有应用程序来处理意图,它将返回null。

如果没有定义协议,则此方法默认为HTTP,因为有些网站没有SSL证书(HTTPS连接所需的证书),如果您尝试使用HTTPS,但没有SSL证书,则这些网站将停止工作。任何网站都可以强制转换为HTTPS,因此无论哪种方式,这些网站都可以让您使用HTTPS


由于此方法使用外部资源来显示页面,因此无需声明INternet权限。显示网页的应用程序必须这样做

其他回答

MarkB的反应是正确的。在我的例子中,我使用的是Xamarin,与C#和Xamarin一起使用的代码是:

var uri = Android.Net.Uri.Parse ("http://www.xamarin.com");
var intent = new Intent (Intent.ActionView, uri);
StartActivity (intent);

该信息来源于:https://developer.xamarin.com/recipes/android/fundamentals/intent/open_a_webpage_in_the_browser_application/

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

你可以试试这个:

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

你也可以走这条路

在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权限。。。

科特林

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

Webview可用于在应用程序中加载Url。用户可以在文本视图中提供URL,也可以对其进行硬编码。

也不要忘记AndroidManifest中的互联网权限。

String url="http://developer.android.com/index.html"

WebView wv=(WebView)findViewById(R.id.webView);
wv.setWebViewClient(new MyBrowser());
wv.getSettings().setLoadsImagesAutomatically(true);
wv.getSettings().setJavaScriptEnabled(true);
wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
wv.loadUrl(url);

private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}