WVActivity.java
3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.lotus.town.notify;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.webkit.DownloadListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.lotus.town.R;
import com.umeng.analytics.MobclickAgent;
public class WVActivity extends Activity{
private WebView mWebview = null;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String url = getIntent().getStringExtra("web_url");
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
setContentView(R.layout.wv_layout);
mWebview = findViewById(R.id.mv_webview);
MobclickAgent.onEvent(WVActivity.this,"m_v_a_p");
initWebView(mWebview,url);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode== KeyEvent.KEYCODE_BACK){
if(this.mWebview.canGoBack()){
this.mWebview.goBack();
return true;
}
}
return super.onKeyDown(keyCode,event);
}
public void initWebView(WebView mWebView, String url){
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setBlockNetworkImage(false);
webSettings.setDomStorageEnabled(true);
/**
* 仅缓存数据
*/
webSettings.setAppCacheEnabled(false);
webSettings.setDatabaseEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setTextSize(WebSettings.TextSize.NORMAL);
//缓存
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
webSettings.setAppCacheMaxSize(1024 * 1024);
String appCacheDir = this.getDir("cache", Context.MODE_PRIVATE).getPath();
webSettings.setDatabasePath(appCacheDir);
webSettings.setSupportZoom(false);// 支持缩放
webSettings.setBuiltInZoomControls(false);
webSettings.setDisplayZoomControls(false);// 取消显示缩放按钮
webSettings.setSupportMultipleWindows(true);// 支持多窗口
webSettings.setSavePassword(false);
mWebView.removeJavascriptInterface("searchBoxJavaBridge_");
mWebView.removeJavascriptInterface("accessibility");
mWebView.removeJavascriptInterface("accessibilityTraversal");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
mWebView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,long contentLength) {
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
MobclickAgent.onEvent(WVActivity.this,"w_v_a_p_f");
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
mWebView.loadUrl(url);
}
}