自定义WebView,支持点击下载链接跳转系统浏览器下载功能,打开本地文件夹


import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.net.Uri
import android.os.Build
import android.util.AttributeSet
import android.webkit.*
import android.widget.Toast


/**
这个WebView只是把系统WebView做了一些封装,并没有改变性能,只是做一次封装,以后使用简单点
 * Note:自定义webview 支持点击下载链接跳转系统浏览器下载功能,
 * 打开本地文件夹(ActivityonActivityResult中调用webView.onActivityResult()方法) * webview内部所有链接默认跳转系统浏览器,可做js交互做其他处理
 * Created by vV on 2018/4/11 8:51.
 * Email:[email protected]
 *
 */

class MyWebView : WebView {
    var uploadMessage: android.webkit.ValueCallback<Array<Uri>>? = null
    private var mUploadMessage: android.webkit.ValueCallback<Uri>? = null
    val REQUEST_SELECT_FILE = 100
    private val FILECHOOSER_RESULTCODE = 2
    lateinit var activity : Activity
    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init()
    }

    private fun init() {
        settings.javaScriptEnabled = true
        webViewClient = object : WebViewClient() {
            override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
                var url1 = request!!.url.path
                if (url1 != null)
                    toSysWeb(url1)
                return true
            }
        }
        setDownloadListener(object : DownloadListener {
            override fun onDownloadStart(p0: String?, p1: String?, p2: String?, p3: String?, p4: Long) {
                toSysWeb(p0!!)
            }

        })
        webChromeClient = object : WebChromeClient() {
            override fun onShowFileChooser(webView: WebView?, filePathCallback: ValueCallback<Array<Uri>>?, fileChooserParams: FileChooserParams?): Boolean {
                if (uploadMessage != null) {
                    uploadMessage!!.onReceiveValue(null)
                    uploadMessage = null
                }
                uploadMessage = filePathCallback
                val intent = fileChooserParams!!.createIntent()
                try {
                    activity?.startActivityForResult(intent, REQUEST_SELECT_FILE)
                } catch (e: ActivityNotFoundException) {
                    uploadMessage = null
                    Toast.makeText(context, "Cannot Open File Chooser", Toast.LENGTH_LONG).show()
                    return false
                }
                return true
            }
        }
    }

    private fun toSysWeb(url: String) {
        val uri = Uri.parse(url)
        val intent = Intent(Intent.ACTION_VIEW, uri)
        context.startActivity(intent)
    }

    fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (requestCode == REQUEST_SELECT_FILE) {
                if (uploadMessage == null)
                    return
                uploadMessage!!.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent))
                uploadMessage = null
            }
        } else if (requestCode == FILECHOOSER_RESULTCODE) {
            if (null == mUploadMessage)
                return
            // Use MainActivity.RESULT_OK if you're implementing WebView inside Fragment
            // Use RESULT_OK only if you're implementing WebView inside an Activity
            val result = if (intent == null || resultCode != Activity.RESULT_OK) null else intent!!.getData()
            mUploadMessage!!.onReceiveValue(result)
            mUploadMessage = null
        } else
            Toast.makeText(context, "Failed to Upload Image", Toast.LENGTH_LONG).show()
    }
}

猜你喜欢

转载自blog.csdn.net/qq_31164261/article/details/79891606