package com.zhong.doule;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.wang.avi.AVLoadingIndicatorView;
public class MainActivity extends AppCompatActivity {
private WebView web;
private View mErrorView; //加载错误的视图
private RelativeLayout mRlLoading;
private AVLoadingIndicatorView mAvView;
@SuppressLint("ResourceAsColor")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
web = findViewById(R.id.webView);
mRlLoading = findViewById(R.id.rl_loading);
mAvView = findViewById(R.id.avi);
web.setBackgroundColor(android.R.color.transparent);
// web.setBackgroundResource(R.mipmap.ic_bg);
initView();
}
protected void initView() {
initErrorPage();
WebSettings webSettings = web.getSettings();
webSettings.setJavaScriptEnabled(true);//是否允许JavaScript脚本运行,默认为false
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);//设置js可以直接打开窗口,如window.open(),默认为false
webSettings.setSupportZoom(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
webSettings.setMediaPlaybackRequiresUserGesture(true);
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setUseWideViewPort(true); // 关键点
webSettings.setAllowFileAccess(true); // 允许访问文件
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
webSettings.setGeolocationEnabled(true);
webSettings.setDomStorageEnabled(true);//开启本地DOM存储
webSettings.setDatabaseEnabled(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); // 不加载缓存内容
// 特别注意
// 每个 Application 只调用一次 WebSettings.setAppCachePath() 和 webSettings.setAppCacheMaxSize();
String cacheDirPath = MainActivity.this.getFilesDir().getAbsolutePath() + "cache/";
webSettings.setAppCachePath(cacheDirPath);
// 1. 设置缓存路径
webSettings.setAppCacheMaxSize(20 * 1024 * 1024);
// 2. 设置缓存大小
webSettings.setAppCacheEnabled(true);
// 3. 开启Application Cache存储机制
/*web.setWebViewClient(new WebViewClient() {
//覆盖shouldOverrideUrlLoading 方法
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("http:") || url.startsWith("https:")) {
view.loadUrl(url);
return false;
} else {
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}catch (Exception e){//防止crash (如果手机上没有安装处理某个scheme开头的url的APP, 会导致crash)
e.printStackTrace();
LogUtil.i("网页错误");
return true;//没有安装该app时,返回true,表示拦截自定义链接,但不跳转,避免弹出上面的错误页面
}
return true;
}
}
});*/
web.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("http:") || url.startsWith("https:")) {
view.loadUrl(url);
return false;
} else {
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
} catch (Exception e) {//防止crash (如果手机上没有安装处理某个scheme开头的url的APP, 会导致crash)
e.printStackTrace();
Log.i("MainActivity", "网页错误");
return true;//没有安装该app时,返回true,表示拦截自定义链接,但不跳转,避免弹出上面的错误页面
}
return true;
}
}
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (MainActivity.this.isFinishing()) {
return;
}
Log.i("MainActivity", "onPageStarted");
web.getSettings().setBlockNetworkImage(true);
//((BaseActivity) mActivity).showLoading(false);
}
@Override
public void onPageFinished(WebView view, String url) {
if (MainActivity.this.isFinishing()) {
return;
}
Log.i("MainActivity", "onPageFinished");
web.getSettings().setBlockNetworkImage(false);
//web.setVisibility(View.VISIBLE);
/// ((BaseActivity) mActivity).dismissLoading();
mAvView.hide();
mRlLoading.setVisibility(View.GONE);
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
Log.i("MainActivity", "onReceivedError: ");
mAvView.hide();
mRlLoading.setVisibility(View.GONE);
showErrorPage();//显示错误页面
}
});
//方式1 webview
web.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (keyCode == KeyEvent.KEYCODE_BACK && web.canGoBack()) { //表示按返回键
web.goBack(); //后退
return true; //已处理
}
}
return false;
}
});
// web.loadUrl("https://m3ws.kugou.com/rank/info/23784");
web.loadUrl("http://47.74.230.207/#/login");
}
/***
* 显示加载失败时自定义的网页
*/
private void initErrorPage() {
if (mErrorView == null) {
mErrorView = View.inflate(MainActivity.this, R.layout.view_web_error, null);
mErrorView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
web.removeView(mErrorView); //添加自定义的错误提示的View
web.loadUrl("http://47.74.230.207/#/login");
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
/**
* 显示自定义错误提示页面,用一个View覆盖在WebView
*/
private void showErrorPage() {
web.removeAllViews(); //移除加载网页错误时,默认的提示信息
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
web.addView(mErrorView, 0, layoutParams); //添加自定义的错误提示的View
}
/**
* 显示自定义错误提示页面,用一个View覆盖在WebView
*/
private void dismissErrorPage() {
// web.removeAllViews(); //移除加载网页错误时,默认的提示信息
// LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (web != null) {
// 如果先调用destroy()方法,则会命中if (isDestroyed()) return;这一行代码,需要先onDetachedFromWindow(),再
// destory()
ViewParent parent = web.getParent();
if (parent != null) {
((ViewGroup) parent).removeView(web);
}
web.stopLoading();
// 退出时调用此方法,移除绑定的服务,否则某些特定系统会报错
web.getSettings().setJavaScriptEnabled(false);
web.clearHistory();
web.clearView();
web.removeAllViews();
web.destroy();
web=null;
}
}
}