之前写了H5,新建assets目录,将文件拷贝导入
然后写个webviewactivity.class,
package practice.lwp.app_gz_zy_sjtl;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.webkit.GeolocationPermissions;
import android.webkit.JavascriptInterface;
import android.webkit.JsPromptResult;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import org.json.JSONException;
import org.json.JSONObject;
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
/**
* liwenpeng
* 2019/5/14 16:56
* App_GZ_ZY_SJTL
* Descrobe:
*/
public class WebViewActivity extends AppCompatActivity {
private WebView mWebView;
private ProgressBar mProgressBar;
private WebSettings settings;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_webview);
initIds();
initWebSettings();
initWebViewSettings();
initData();
}
private void initData() {
mWebView.loadUrl("file:///android_asset/index.html");
}
private void initWebViewSettings() {
mWebView.setWebViewClient(mWebViewClient);
mWebView.setWebChromeClient(mWebChromeClient);
mWebView.addJavascriptInterface(new SJTLJavaScriptInterfacec(),"android");
}
private class SJTLJavaScriptInterfacec {
@JavascriptInterface
public void goBack() {
WebViewActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mWebView.canGoBack()){
mWebView.goBack();
Log.d("klwp","canGoBack");
}else {
Log.d("klwp","else");
finish();
}
}
});
}
}
private void initWebSettings() {
settings = mWebView.getSettings();
settings.setBuiltInZoomControls(true);
settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
settings.setSupportZoom(true);
settings.setSupportMultipleWindows(true);
settings.setAppCacheEnabled(true);
//居中
settings.setUseWideViewPort(true);
settings.setLoadWithOverviewMode(true);
settings.setSavePassword(false);
settings.setSaveFormData(false);
settings.setAllowFileAccess(true);
settings.setGeolocationEnabled(true);
settings.setAppCacheMaxSize(Long.MAX_VALUE);
settings.setDomStorageEnabled(true);
settings.setPluginState(WebSettings.PluginState.ON_DEMAND);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
String dir = this.getDir("database", Context.MODE_PRIVATE).getPath();
//设置定位的数据库路径
settings.setGeolocationDatabasePath(dir);
settings.setJavaScriptCanOpenWindowsAutomatically(true);
settings.setJavaScriptEnabled(true);
settings.setBlockNetworkImage(false);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
}
private void initIds() {
mWebView = findViewById(R.id.main_webview);
mProgressBar = findViewById(R.id.pb_brower);
}
//WebViewClient主要帮助WebView处理各种通知、请求事件
private WebViewClient mWebViewClient = new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {//页面加载完成
mProgressBar.setVisibility(View.GONE);
}
@Override
public void onReceivedSslError(WebView view, android.webkit.SslErrorHandler handler, android.net.http.SslError error) {
// super.onReceivedSslError(view, handler, error);
handler.proceed();
}
@Override
public void onPageStarted(WebView webView, String s, Bitmap bitmap) {
mProgressBar.setVisibility(View.VISIBLE);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// if (!url.startsWith("http") & !url.startsWith("https")) {
// return false;
// } else {
view.loadUrl(url);
return true;
// }
}
};
//WebChromeClient主要辅助WebView处理Javascript的对话框、网站图标、网站title、加载进度等
private WebChromeClient mWebChromeClient = new WebChromeClient() {
@Override
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin,true,false);
super.onGeolocationPermissionsShowPrompt(origin, callback);
}
@Override
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
return super.onJsPrompt(view, url, message, defaultValue, result);
}
@Override
public void onProgressChanged(WebView view, int newProgress) {
if (newProgress == 100) {
mProgressBar.setVisibility(GONE);
} else {
if (mProgressBar.getVisibility() == GONE)
mProgressBar.setVisibility(VISIBLE);
mProgressBar.setProgress(newProgress);
}
super.onProgressChanged(view, newProgress);
}
};
@Override
public void onBackPressed() {
if (mWebView.canGoBack()){
mWebView.goBack();
}
else
finish();
}
}
这边我用的是h5的返回,在h5那边要处理下,比如:
当点击图片后调用android这个对象的goBack方法,这个对象我在webviewactivity定义了:
mWebView.addJavascriptInterface(new SJTLJavaScriptInterfacec(),"android");//android即为h5调用android对象
private class SJTLJavaScriptInterfacec {
@JavascriptInterface
public void goBack() {
WebViewActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if (mWebView.canGoBack()){
mWebView.goBack();
Log.d("klwp","canGoBack");
}else {
Log.d("klwp","else");
finish();
}
}
});
}
}
最后有个问题,h5无法访问到网络,经测试,在6.0手机是可以的,那就是版本兼容性问题(http和https的安全性问题),
解决:
第一步:新建xml文件夹,再新建一个文件
network_security_config.xml:
第2步:
AndroidManifest的application里李加上:
android:networkSecurityConfig="@xml/network_security_config"
最后注意别忘了加网络权限.然后就能访问了