0


测试Android webview 加载本地html

最近开发一个需要未联网功能的App, 不熟悉使用Java原生开发界面,于是想使用本地H5做界面,本文测试了使用本地html加载远程数据。直接上代码:

MainActivity.java

packagecom.alex.webviewlocal;importandroidx.appcompat.app.AppCompatActivity;importandroid.os.Build;importandroid.os.Bundle;importandroid.webkit.CookieManager;importandroid.webkit.WebResourceRequest;importandroid.webkit.WebSettings;importandroid.webkit.WebView;importandroid.webkit.WebViewClient;importjava.lang.reflect.InvocationTargetException;importjava.lang.reflect.Method;publicclassMainActivityextendsAppCompatActivity{privateWebView webView;privateString url="file:///android_asset/web/index.html";@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);
        webView =findViewById(R.id.webview);WebSettings webSettings = webView.getSettings();CookieManager cookieManager =CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        webSettings.setJavaScriptEnabled(true);// 设置支持javascript
        webSettings.setUseWideViewPort(true);// 将图片调整到适合webview的大小
        webSettings.setLoadWithOverviewMode(true);// 缩放至屏幕的大小
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setUserAgentString("User-Agent");
        webSettings.setLightTouchEnabled(true);// 设置用鼠标激活被选项
        webSettings.setBuiltInZoomControls(true);// 设置支持缩放
        webSettings.setDomStorageEnabled(true);//设置DOM缓存,当H5网页使用localstorage时,一定要设置
        webSettings.setDatabaseEnabled(true);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);// 设置去缓存,防止加载的为上一次加载的数据
        webSettings.setSupportZoom(true);// 设置支持变焦
        webView.setHapticFeedbackEnabled(false);
        webSettings.setPluginState(WebSettings.PluginState.ON);
        webSettings.setAllowFileAccess(true);
        webSettings.setAllowContentAccess(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        webSettings.setAllowFileAccessFromFileURLs(true);

        webView.loadUrl(url);//        try{//            if(Build.VERSION.SDK_INT>=16){//                Class<?> clazz = webView.getSettings().getClass();//                Method method = clazz.getMethod(//                        "setAllowUniversalAccessFromFileURLs", boolean.class);//                if(method!=null){//                    method.invoke(webView.getSettings(),true);//                }//            }//        } catch (NoSuchMethodException e) {//            throw new RuntimeException(e);//        } catch (InvocationTargetException e) {//            throw new RuntimeException(e);//        } catch (IllegalAccessException e) {//            throw new RuntimeException(e);//        }////        webView.loadUrl(url);//        webView.setWebViewClient(new WebViewClient(){//            @Override//            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {//                return super.shouldOverrideUrlLoading(view, request);//            }//        });}}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><WebViewandroid:id="@+id/webview"android:layout_width="match_parent"android:layout_height="match_parent"></WebView></LinearLayout>

h5 文件

<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>Document</title><scriptsrc="./vue.min.js"></script><scriptsrc="./axios.min.js"></script></head><body><divid="app"><h2>{{message}}</h2><ul><liv-for="user in users":key="user.id">{{user.name}}</li></ul></div><script>var app =newVue({el:'#app',data(){return{message:'Hello Vue!',users:[]}},mounted(){
                axios.get('https://jsonplaceholder.typicode.com/users').then(response=>{this.users = response.data
                }).catch(error=>{
                    console.log(error)})}})</script></body></html>

在这里插入图片描述
最终效果:
在这里插入图片描述

标签: android html 前端

本文转载自: https://blog.csdn.net/xingkongtianyuzhao/article/details/133955192
版权归原作者 星空你好 所有, 如有侵权,请联系我们删除。

“测试Android webview 加载本地html”的评论:

还没有评论