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

当前回答

kolin扩展函数。

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

其他回答

在2.3中,我有更好的运气

final Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(url));
activity.startActivity(intent);

区别在于使用Intent.ACTION_VIEW而不是字符串“android.Intent.ACTION.VIEW”

通过意图查看简单的网站,

Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://www.yoursite.in"));
startActivity(viewIntent);  

使用这个简单的代码在android应用程序中查看您的网站。

在清单文件中添加internet权限,

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

android.webkit.URLUtil自Api级别1(android 1.0)以来,guessUrl(String)方法工作得非常好(即使使用file://或data://)。用作:

String url = URLUtil.guessUrl(link);

// url.com            ->  http://url.com/     (adds http://)
// http://url         ->  http://url.com/     (adds .com)
// https://url        ->  https://url.com/    (adds .com)
// url                ->  http://www.url.com/ (adds http://www. and .com)
// http://www.url.com ->  http://www.url.com/ 
// https://url.com    ->  https://url.com/
// file://dir/to/file ->  file://dir/to/file
// data://dataline    ->  data://dataline
// content://test     ->  content://test

在“活动”调用中:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(URLUtil.guessUrl(download_link)));

if (intent.resolveActivity(getPackageManager()) != null)
    startActivity(intent);

有关详细信息,请查看完整的guessUrl代码。

你也可以走这条路

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

尝试此代码

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
   package="com.example.myapplication5">

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

    <application
    android:usesCleartextTraffic="true"
    android:allowBackup="true"
    .....
     />
     <activity android:name=".MainActivity"
        android:screenOrientation="portrait"
        tools:ignore="LockedOrientationActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
 </manifest>

主要活动.java

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {
    private WebView mWebview;
    String link = "";// global variable
    Resources res;// global variable

    @Override


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.home);

        loadWebPage();
    }

    public void loadWebPage()
    {
        mWebview = (WebView) findViewById(R.id.webView);
        WebSettings webSettings = mWebview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        final Activity activity = this;
        mWebview.setWebViewClient(new WebViewClient() {
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
            }
        });
        mWebview.loadUrl("http://www.google.com");

    }

    public void reLoad(View v)
    {
        loadWebPage();
    }
}

布局.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textView"
        android:layout_width="335dp"
        android:layout_height="47dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="9dp"
        android:layout_marginTop="8dp"
        android:paddingLeft="10dp"
        android:paddingTop="5dp"
        android:text="URL : https://ktmmovie.co/"
        android:textSize="18dp"
        android:layout_marginLeft="9dp"
        android:layout_alignParentLeft="true" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginStart="7dp"
        android:layout_marginLeft="7dp"
        android:layout_marginEnd="8dp"
        android:layout_toEndOf="@+id/textView"
        android:layout_toRightOf="@+id/textView"
        android:clickable="true"
        android:src="@android:drawable/ic_popup_sync"
        android:layout_marginRight="8dp"
        android:layout_alignParentRight="true"
        android:onClick="reLoad"/>

    <WebView
        android:id="@+id/webView"
        android:layout_width="401dp"
        android:layout_height="665dp"
        android:layout_below="@+id/textView"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="3dp"
        android:layout_marginLeft="3dp"
        android:layout_marginTop="3dp"
        android:layout_marginBottom="7dp" />


</RelativeLayout>