164 lines
6.3 KiB
Kotlin
164 lines
6.3 KiB
Kotlin
package com.arpa.hndahesudintocctmsdriver.base
|
||
|
||
import android.os.Bundle
|
||
import android.text.TextUtils
|
||
import android.view.View
|
||
import android.webkit.WebView
|
||
import android.widget.FrameLayout
|
||
import android.widget.TextView
|
||
import com.arpa.hndahesudintocctmsdriver.databinding.ActivityWebBinding
|
||
import com.arpa.hndahesudintocctmsdriver.utils.SPUtils
|
||
import com.arpa.mylibrary.R
|
||
import com.arpa.mylibrary.base.BaseActivity
|
||
import com.just.agentweb.AgentWeb
|
||
import com.just.agentweb.AgentWebUIControllerImplBase
|
||
import com.just.agentweb.WebChromeClient
|
||
|
||
/**
|
||
* @ClassName WebActivity
|
||
* @Author john
|
||
* @Date 2024/2/1 15:08
|
||
* @Description TODO
|
||
*/
|
||
class WebRichTextActivity : BaseActivity<ActivityWebBinding>() {
|
||
|
||
var mTextRight: TextView? = null
|
||
private var mAgentWeb: AgentWeb? = null
|
||
|
||
private var mTitle = ""
|
||
private var mUrl = ""
|
||
|
||
override fun initView(savedInstanceState: Bundle?) {
|
||
mTitle = intent.extras?.getString("title").toString()
|
||
mUrl = intent.extras?.getString("url").toString()
|
||
|
||
setStatusBarColor(R.color.colorWhite)
|
||
setTitleBar(mTitle) { v: View? -> if (!mAgentWeb!!.back()) finish() }
|
||
|
||
mAgentWeb = AgentWeb.with(this)
|
||
.setAgentWebParent(binding.WebViewLayout, FrameLayout.LayoutParams(-1, -1))
|
||
.useDefaultIndicator()
|
||
.setWebChromeClient(mWebChromeClient)
|
||
.setSecurityType(AgentWeb.SecurityType.DEFAULT_CHECK)
|
||
.setAgentWebUIController(AgentWebUIControllerImplBase())
|
||
.createAgentWeb()
|
||
.ready()
|
||
.go(mUrl)
|
||
if (mUrl == "https://support.qq.com/products/335639") {
|
||
val userInfo = SPUtils.instance.getUserInfo(mContext)
|
||
var postData =
|
||
"""nickname=${if (userInfo?.idcardName.isNullOrEmpty()) "游客" else userInfo?.idcardName}&avatar=${userInfo?.avatar}&openid=${userInfo?.contactPhone}"""
|
||
mAgentWeb?.webCreator?.webView?.postUrl(mUrl, postData.toByteArray())
|
||
}
|
||
|
||
// mAgentWeb?.jsInterfaceHolder?.addJavaObject("phone", AndroidInterfaceWeb())
|
||
}
|
||
|
||
override fun initDate() {
|
||
|
||
}
|
||
|
||
fun initOpinion() {
|
||
binding.run {
|
||
// val ub: UserBean? = SPUtils.instance.getUserInfo(mContext)
|
||
// wv.getSettings().setJavaScriptEnabled(true)
|
||
// wv.getSettings().setDomStorageEnabled(true) // 这个要加上
|
||
// val openid: String = ub.getData().getUname() // 用户的openid
|
||
// val nickname: String = StringUtil.isNull(ub.getData().getRname(), "游客") // 用户的nickname
|
||
// val headimgurl: String =
|
||
// StringUtil.isNull(ub.getData().getHeadportraitUrl(), "") // 用户的头像url
|
||
//
|
||
// /* 获得 webview url,请注意url单词是product而不是products,products是旧版本的参数,用错地址将不能成功提交 */
|
||
// val url = "https://support.qq.com/products/335639" // 把1221数字换成你的产品ID,否则会不成功
|
||
// /* 准备post参数 */
|
||
// val postData =
|
||
// "nickname=$nickname&avatar=$headimgurl&openid=$openid"
|
||
// wv.postUrl(url, postData.toByteArray())
|
||
// wv.setWebChromeClient(chromeClient)
|
||
// wv.setWebViewClient(object : android.webkit.WebViewClient() {
|
||
// override fun shouldOverrideUrlLoading(
|
||
// view: WebView,
|
||
// request: WebResourceRequest
|
||
// ): Boolean {
|
||
// return super.shouldOverrideUrlLoading(view, request)
|
||
// }
|
||
//
|
||
// override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
|
||
// //返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器
|
||
// view.loadUrl(url)
|
||
// return true
|
||
// }
|
||
// })
|
||
}
|
||
|
||
}
|
||
|
||
private val mWebChromeClient: WebChromeClient = object : WebChromeClient() {
|
||
override fun onProgressChanged(view: WebView, newProgress: Int) {
|
||
super.onProgressChanged(view, newProgress)
|
||
}
|
||
|
||
override fun onReceivedTitle(view: WebView, title: String) {
|
||
super.onReceivedTitle(view, title)
|
||
setTitleBar(
|
||
if (TextUtils.isEmpty(mTitle)) title else mTitle
|
||
) { if (!mAgentWeb!!.back()) finish() }
|
||
}
|
||
}
|
||
|
||
// private val mWebViewClient: WebViewClient = object : WebViewClient() {
|
||
// override fun onPageStarted(view: WebView, url: String, favicon: Bitmap) {
|
||
// super.onPageStarted(view, url, favicon)
|
||
// }
|
||
//
|
||
// override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
|
||
// return super.shouldOverrideUrlLoading(view, request)
|
||
// } // @Override
|
||
// // public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
|
||
// // //该方法在Build.VERSION_CODES.LOLLIPOP以后有效
|
||
// // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||
// // String url = request.getUrl().toString();
|
||
// // Log.i(TAG, "shouldOverrideUrlLoading: " + url);
|
||
// // }
|
||
// // return false;
|
||
// // }
|
||
// }
|
||
|
||
|
||
// @OnClick({R.id.common_toolBar_text_right, R.id.common_toolBar_image_right})
|
||
// public void onClick(View view) {
|
||
// if (view.getId() == R.id.common_toolBar_text_right) {
|
||
// } else if (view.getId() == R.id.common_toolBar_image_right) {
|
||
// }
|
||
// }
|
||
override fun onPause() {
|
||
mAgentWeb?.webLifeCycle?.onPause()
|
||
super.onPause()
|
||
}
|
||
|
||
override fun onResume() {
|
||
mAgentWeb?.webLifeCycle?.onResume()
|
||
super.onResume()
|
||
}
|
||
|
||
override fun onDestroy() {
|
||
mAgentWeb?.webLifeCycle?.onDestroy()
|
||
super.onDestroy()
|
||
}
|
||
|
||
override fun onBackPressed() {
|
||
// super.onBackPressed()
|
||
if (!mAgentWeb!!.back()) finish()
|
||
}
|
||
|
||
|
||
// internal class AndroidInterfaceWeb {
|
||
// @JavascriptInterface
|
||
// fun gohome() {
|
||
// val intent = Intent(mContext, HomeActivity::class.java)
|
||
// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
|
||
// mContext.startActivity(intent)
|
||
// }
|
||
// }
|
||
|
||
} |