lmw
2023-06-13 4b7d8d9a038f6522df46d0f14fa07eb940a1b34d
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package com.driver.emanagercar.ui.base
 
import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.webkit.*
import cn.sinata.xldutils.utils.Utils
import com.kuanzhai.driver.R
import com.kuanzhai.driver.base.Const
import com.kuanzhai.driver.base.InnerJsInterface
import com.kuanzhai.driver.base.MyBaseActivity
import kotlinx.android.synthetic.main.activity_webview.*
 
/**
 * webview页面
 * Created by Administrator on 2018/1/18.
 */
class BaseWebActivity : MyBaseActivity() {
 
    var state = 0
 
    val url by lazy {
        intent.extras!!.getString(Const.ShowIntent.URL)
    }
 
    val title by lazy {
        intent.extras!!.getString(Const.ShowIntent.TITLE)
    }
 
    val hint by lazy {
        intent.extras!!.getString("hint")
    }
 
    override fun setContentView() {
        setContentView(R.layout.activity_webview)
    }
 
    override fun initView() {
        setTitleText(title)
        setWebview()
        if (!hint.isNullOrEmpty()){
            tv_hint_bottom.visibility = View.VISIBLE
        }
    }
 
    @SuppressLint("SetJavaScriptEnabled")
    private fun setWebview() {
        val settings = webView!!.settings
        settings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
        settings.defaultTextEncodingName = "utf-8" //设置编码格式
        settings.useWideViewPort = true //设置此属性,可任意比例缩放
        settings.loadWithOverviewMode = true // 页面支持缩放:
        settings.builtInZoomControls = true
        settings.useWideViewPort = false //将图片调整到适合webview的大小
        settings.setSupportZoom(false) //支持缩放
        settings.supportMultipleWindows() //多窗口
        settings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK //关闭webview中缓存
        settings.allowFileAccess = true //设置可以访问文件
        settings.setNeedInitialFocus(true) //当webview调用requestFocus时为webview设置节点
        settings.javaScriptCanOpenWindowsAutomatically = true //支持通过JS打开新窗口
        settings.loadsImagesAutomatically = true //支持自动加载图片
        settings.javaScriptEnabled = true
        settings.domStorageEnabled = true
        webView!!.isScrollContainer = false
        webView!!.isScrollbarFadingEnabled = false
        webView!!.scrollBarStyle = View.SCROLLBARS_OUTSIDE_OVERLAY
        webView!!.addJavascriptInterface(InnerJsInterface(), "HTMLOUT")
        webView!!.webChromeClient = object : WebChromeClient() {
            override fun onReceivedTitle(
                view: WebView?,
                title: String?
            ) {
                super.onReceivedTitle(view, title)
                setTitle(title)
            }
        }
        webView!!.webViewClient = object : WebViewClient() {
            override fun onPageFinished(
                view: WebView?,
                url: String?
            ) {
                super.onPageFinished(view, url)
                webView!!.loadUrl("javascript:HTMLOUT.processHTML(document.documentElement.outerHTML);")
                if (isDestroy) {
                    return
                }
                ll_loading!!.visibility = View.GONE
            }
 
            override fun onPageStarted(
                view: WebView?,
                url: String?,
                favicon: Bitmap?
            ) {
                super.onPageStarted(view, url, favicon)
                ll_loading!!.visibility = View.VISIBLE
            }
 
            override fun shouldOverrideUrlLoading(
                view: WebView?,
                url: String?
            ): Boolean {
                Utils.systemErr(url)
                if (!TextUtils.isEmpty(url)) {
                    webView!!.loadUrl(url)
                }
                return true
            }
 
            override fun shouldInterceptRequest(
                view: WebView?,
                request: WebResourceRequest?
            ): WebResourceResponse? {
                return super.shouldInterceptRequest(view, request)
            }
        }
        if (url!!.startsWith("http")) {
            webView!!.loadUrl(url)
        } else {
            val sHead =
                "<html><head><meta name=\"viewport\" content=\"width=device-width, " + "initial-scale=1.0, minimum-scale=0.5, maximum-scale=2.0, user-scalable=yes\" />" + "<style>img{max-width:100% !important;height:auto !important;}</style>" + "<style>body{max-width:100% !important;}</style>" + "</head><body>"
            webView!!.loadDataWithBaseURL(null, sHead + url, "text/html", "utf-8", null)
        }
    }
 
    override fun setOnclick() {}
    protected fun resetTitle(): Boolean {
        return true
    }
 
    override fun onDestroy() {
        super.onDestroy()
        //webView回收,部分手机无效
        if (webView != null) {
            webView!!.stopLoading()
            webView!!.removeAllViews()
            webView!!.destroy()
        }
    }
 
 
    companion object {
        fun to(context: Context,url: String?, title: String?) {
            val bundle = Bundle()
            bundle.putString(Const.ShowIntent.URL, url)
            bundle.putString(Const.ShowIntent.TITLE, title)
            var intent = Intent(context,BaseWebActivity::class.java)
            intent.putExtras(bundle)
            context.startActivity(intent)
        }
 
        fun to(context: Context,url: String?, title: String?,hint : String) {
            val bundle = Bundle()
            bundle.putString(Const.ShowIntent.URL, url)
            bundle.putString(Const.ShowIntent.TITLE, title)
            bundle.putString("hint", hint)
            var intent = Intent(context,BaseWebActivity::class.java)
            intent.putExtras(bundle)
            context.startActivity(intent)
        }
    }
}