package com.example.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.zhaoyang.driver.R
|
import com.zhaoyang.driver.base.Const
|
import com.zhaoyang.driver.base.InnerJsInterface
|
import com.zhaoyang.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)
|
}
|
}
|
}
|