欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android使用webView加載html頁面的詳細(xì)步驟

 更新時(shí)間:2024年06月12日 10:44:58   作者:破z曉  
Android WebView是Android開發(fā)中提供的一種用于顯示網(wǎng)頁內(nèi)容的組件,它可以加載網(wǎng)頁的url鏈接,也可以加載本地的html文件,下面這篇文章主要給大家介紹了關(guān)于Android使用webView加載html頁面的相關(guān)資料,需要的朋友可以參考下

1、首先在布局xml里面指定WebView根節(jié)點(diǎn)

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

2、在.java的onCreate()里使用

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_three);

        //1. asset目錄下的index.html文件
        String filePath = "file:///android_asset/html/index.html";
        //2.本地內(nèi)存中的index.html文件
        // 獲取文件夾路徑
        String htmlPath = getExternalFilesDir("html").getPath();
        File htmlFile = new File(htmlPath);
        // 判斷是否存在,不存在則創(chuàng)建
        if (htmlFile.exists()){
            htmlPath =  htmlFile.getPath()+File.separator+"index.html";
        }else {
            htmlFile.mkdirs();
            htmlPath =  htmlFile.getPath()+File.separator+"index.html";
        }
        // 地址
        String localFilePath = "file:///"+htmlPath;
        //3.指定的URL的html文件
        /**
         * 若是不顯示,在AndroidManifest.xml中添加android:usesCleartextTraffic="true"
         * 并且設(shè)置網(wǎng)絡(luò)權(quán)限
         */
        String urlPath = "https://www.baidu.com/";


        myWebView = findViewById(R.id.myWebView);
        WebSettings myWebSettings = myWebView.getSettings();
        // webView解決加載html頁面空白問題
        myWebSettings.setJavaScriptEnabled(true);// 設(shè)置支持javascript
        myWebSettings.setUseWideViewPort(true);//將圖片調(diào)整到適合webView大小
        myWebSettings.setLoadWithOverviewMode(true);//縮放至屏幕大小
        myWebSettings.setDomStorageEnabled(true);//設(shè)置DOM緩存,當(dāng)H5網(wǎng)頁使用localstorage時(shí)一定要設(shè)置
        myWebSettings.setCacheMode(android.webkit.WebSettings.LOAD_NO_CACHE);// 設(shè)置去緩存,防止加載的是上一次數(shù)據(jù)
        myWebSettings.setDatabaseEnabled(true);

        // 解決加載本地內(nèi)存中報(bào)錯(cuò) err_access_denied
        myWebSettings.setAllowFileAccess(true);
        myWebSettings.setAllowContentAccess(true);

        // 解決webView報(bào)錯(cuò) Loading local files from file:// urls is not possible due browser security restrictions
        /**
        *  設(shè)置是否允許運(yùn)行在一個(gè)file schema URL環(huán)境下的JavaScript訪問來自其他任何來源的內(nèi)容,
         * 包括其他file schema URLs。
         * 通過此API可以設(shè)置是否允許通過file url加載的Javascript可以訪問其他的源,
         * 包括其他的文件和http,https等其他的源。與上面的類似,實(shí)現(xiàn)一個(gè)就可以。
         * webSetting.setAllowUniversalAccessFromFileURLs(true);
        * */
        myWebSettings.setAllowUniversalAccessFromFileURLs(true);
        /**
         * 設(shè)置是否允許運(yùn)行在一個(gè)file schema URL環(huán)境下的JavaScript訪問來自其他任何來源的內(nèi)容,
         * 包括其他file schema URLs。
         * 通過此API可以設(shè)置是否允許通過file url加載的Javascript可以訪問其他的源,
         * 包括其他的文件和http,https等其他的源。與上面的類似,實(shí)現(xiàn)一個(gè)就可以。
         */
        //myWebSettings.setAllowUniversalAccessFromFileURLs(true);


        //加載html
        if (filePath != null) {
            myWebView.loadUrl(urlPath);
        }
    }

3、創(chuàng)建assets目錄(與res目錄同一級(jí)別)

4、將要訪問的*.html頁面放置到assets目錄即可 

5、使用X5內(nèi)核 騰訊SDK 

地址:騰訊瀏覽服務(wù)

下載sdk:騰訊瀏覽服務(wù)-SDK下載

放置在libs文件夾,引用

AS高版本:

implementation(fileTree("libs"))

AS低版本:

android{
    ...
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }
}
 

dependencies{
    ...
    compile files('libs/tbs_sdk_thirdapp_v4.3.0.386_44286_sharewithdownloadwithfile_withoutGame_obfs_20230210_114429.jar')
}

AndroidManifest.xml配置權(quán)限

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

 <application
        android:name=".activity.app.MyAplication"
        ***
/application>

Application.java設(shè)置初始化

package com.example.yuanzhoulv.activity.app;;
import android.app.Application;
import com.tencent.smtt.sdk.QbSdk;

public class MyAplication extends Application {

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        //搜集本地tbs內(nèi)核信息并上報(bào)服務(wù)器,服務(wù)器返回結(jié)果決定使用哪個(gè)內(nèi)核。

        QbSdk.PreInitCallback cb = new QbSdk.PreInitCallback() {

            @Override
            public void onViewInitFinished(boolean arg0) {
                // TODO Auto-generated method stub
                //x5內(nèi)核初始化完成的回調(diào),為true表示x5內(nèi)核加載成功,否則表示x5內(nèi)核加載失敗,會(huì)自動(dòng)切換到系統(tǒng)內(nèi)核。
            }

            @Override
            public void onCoreInitFinished() {
                // TODO Auto-generated method stub
            }
        };
        //x5內(nèi)核初始化接口
        QbSdk.initX5Environment(getApplicationContext(),  cb);
    }
}

使用:

*.xml

  <com.tencent.smtt.sdk.WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

*.java

        //1. asset目錄下的index.html文件
        String filePath = "file:///android_asset/html/index.html";
        //2.本地內(nèi)存中的index.html文件
        // 獲取文件夾路徑
        String htmlPath = getExternalFilesDir("html").getPath();
        File htmlFile = new File(htmlPath);
        // 判斷是否存在,不存在則創(chuàng)建
        if (htmlFile.exists()){
            htmlPath =  htmlFile.getPath()+File.separator+"index.html";
        }else {
            htmlFile.mkdirs();
            htmlPath =  htmlFile.getPath()+File.separator+"index.html";
        }
        // 地址
        String localFilePath = "file:///"+htmlPath;
        //3.指定的URL的html文件
        /**
         * 若是不顯示,在AndroidManifest.xml中添加android:usesCleartextTraffic="true"
         * 并且設(shè)置網(wǎng)絡(luò)權(quán)限
         */
        String urlPath = "https://www.baidu.com/";

webView = findViewById(R.id.webView);
        com.tencent.smtt.sdk.WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);// 設(shè)置支持javascript
        webSettings.setUseWideViewPort(true);//將圖片調(diào)整到適合webView大小
        webSettings.setLoadWithOverviewMode(true);//縮放至屏幕大小
        webSettings.setDomStorageEnabled(true);//設(shè)置DOM緩存,當(dāng)H5網(wǎng)頁使用localstorage時(shí)一定要設(shè)置
        webSettings.setCacheMode(android.webkit.WebSettings.LOAD_NO_CACHE);// 設(shè)置去緩存,防止加載的是上一次數(shù)據(jù)
        webSettings.setDatabaseEnabled(true);

        // 解決加載本地內(nèi)存中報(bào)錯(cuò) err_access_denied
        webSettings.setAllowFileAccess(true);
        webSettings.setAllowContentAccess(true);

        webSettings.setAllowUniversalAccessFromFileURLs(true);

        //加載html
        if (filePath != null) {
            webView.loadUrl(localFilePath);
        }

總結(jié) 

到此這篇關(guān)于Android使用webView加載html頁面的文章就介紹到這了,更多相關(guān)Android用webView加載html頁面內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論