記錄為主舷丹,直接上代碼吧械念。
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必須設(shè)置支持Javascript
? ? ? ? settings.setDomStorageEnabled(true);//主要是這句
? ? ? ? settings.setJavaScriptEnabled(true);
//android?webview 從Lollipop(5.0)開始webview默認(rèn)不允許混合模式,https當(dāng)中不能加載http資源拂盯,而開發(fā)的
? ? ? ? //時候可能使用的是https的鏈接实束,但是鏈接中的圖片可能是http的,所以需要設(shè)置開啟歉备。
? ? ? ? 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;
}
});
//該方法解決的問題是打開瀏覽器不調(diào)用系統(tǒng)瀏覽器傅是,直接用webview打開
? ? ? ? webView.setWebChromeClient(new WebChromeClient());//這行最好不要丟掉
? ? ? ? webView.setWebViewClient(new WebViewClient() {
@Override
? ? ? ? ? ? public void onReceivedSslError(WebView view,SslErrorHandler handler, SslError error) {
//等待證書響應(yīng)
? ? ? ? ? ? ? ? handler.proceed();
}
});
//需要加載的網(wǎng)頁的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" >
<!--顯示網(wǎng)頁區(qū)域-->
? ? ? ? android:id="@+id/webView"
? ? ? ? android:layout_below="@+id/text_endLoading"
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="fill_parent"
? ? ? ? android:layout_marginTop="10dp" />
權(quán)限設(shè)置
<?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">
總結(jié):在搞這個小功能過程中主要遇到的問題是加載不出圖片,原因是網(wǎng)是https,但是圖片資源卻是http喧笔,android的webview不支持混合調(diào)用帽驯,所以得做相關(guān)處理,上面都有寫注釋了书闸。