美文网首页
Adnroid打开网页-打包app来体验手机网站

Adnroid打开网页-打包app来体验手机网站

作者: 蒙双眼看世界 | 来源:发表于2020-06-29 18:14 被阅读0次

    记录为主,直接上代码吧。

    package com.example.myapplication;

    import android.app.Activity;

    import android.net.http.SslError;

    import android.os.Build;

    import android.os.Bundle;

    import android.webkit.SslErrorHandler;

    import android.webkit.WebChromeClient;

    import android.webkit.WebSettings;

    import android.webkit.WebView;

    import android.webkit.WebViewClient;

    public class FullscreenActivityextends Activity {

    private WebViewwebView;

    @Override

        protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_fullscreen);

    init();

    }

    private void init(){

    webView = (WebView) findViewById(R.id.webView);

    WebSettings settings =webView.getSettings();

    // 如果访问的页面中要与Javascript交互,则webview必须设置支持Javascript

            settings.setDomStorageEnabled(true);//主要是这句

            settings.setJavaScriptEnabled(true);

    //android webview 从Lollipop(5.0)开始webview默认不允许混合模式,https当中不能加载http资源,而开发的

            //时候可能使用的是https的链接,但是链接中的图片可能是http的,所以需要设置开启。

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

    settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

    }

    settings.setBlockNetworkImage(false);//解决图片不显示

            settings.setJavaScriptCanOpenWindowsAutomatically(true);

    settings.setLoadsImagesAutomatically(true);

    webView.setWebViewClient(new WebViewClient(){

    public boolean shouldOverrideUrlLoading(WebView view, String url){

    view.loadUrl(url);

    return true;

    }

    });

    //该方法解决的问题是打开浏览器不调用系统浏览器,直接用webview打开

            webView.setWebChromeClient(new WebChromeClient());//这行最好不要丢掉

            webView.setWebViewClient(new WebViewClient() {

    @Override

                public void onReceivedSslError(WebView view,SslErrorHandler handler, SslError error) {

    //等待证书响应

                    handler.proceed();

    }

    });

    //需要加载的网页的url

            webView.loadUrl("https://test.gobricks.cn/");

    }

    }

    XML布局

    <?xml version="1.0" encoding="utf-8"?>

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:orientation="vertical" >

    <!--显示网页区域-->

            android:id="@+id/webView"

            android:layout_below="@+id/text_endLoading"

            android:layout_width="fill_parent"

            android:layout_height="fill_parent"

            android:layout_marginTop="10dp" />

    权限设置

    <?xml version="1.0" encoding="utf-8"?>

        xmlns:tools="http://schemas.android.com/tools"

        package="com.example.myapplication">

            android:allowBackup="true"

            android:icon="@mipmap/ic_launcher"

            android:label="@string/app_name"

            android:roundIcon="@mipmap/ic_launcher_round"

            android:supportsRtl="true"

            android:theme="@style/AppTheme"

            android:usesCleartextTraffic="true"

            >

                android:name=".FullscreenActivity"

                android:configChanges="orientation|keyboardHidden|screenSize"

                android:label="@string/app_name"

                android:theme="@style/FullscreenTheme">

    总结:在搞这个小功能过程中主要遇到的问题是加载不出图片,原因是网是https,但是图片资源却是http,android的webview不支持混合调用,所以得做相关处理,上面都有写注释了。

    相关文章

      网友评论

          本文标题:Adnroid打开网页-打包app来体验手机网站

          本文链接:https://www.haomeiwen.com/subject/xgagmhtx.html