自定义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:759889091@qq.com
 *
 */

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()
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值