Lollipop和棉花糖

时间:2019-01-26 12:09:36

标签: android android-webview backwards-compatibility

我已经在android 7.0上尝试过,它可以工作,但是在android 5,6上却不起作用,我找不到任何可能出问题的线索。 该URL重定向到付款门户,最后它从Javascript调用Android.postMessage(“ success”),但未在本机中调​​用,此问题仅在android 5中存在,也许在android 6中也存在。

我的最低SDK级别为21。

class PaymentActivity : BaseActivity<ActivityPaymentBinding, IPaymentMvvm.ViewModel>(), IPaymentMvvm.View {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activityComponent.inject(this)
        setAndBindContentView(savedInstanceState, R.layout.activity_payment)
        viewModel.initVM()
        binding.web.webViewClient = Web(viewModel)
        binding.web.settings.javaScriptEnabled = true
        binding.web.settings.loadWithOverviewMode = true
        binding.web.settings.useWideViewPort = true
        binding.web.addJavascriptInterface(JSBridge(this, this), "Android")


        val url = intent.extras.getString("url")
        if (url == null)
            finish()

   binding.web.loadUrl(url)




    }

    override fun onOptionsItemSelected(menuItem: MenuItem): Boolean {

        when (menuItem.itemId) {
            android.R.id.home ->
                finish()

        }
        return super.onOptionsItemSelected(menuItem)
    }

    class Web(val viewModel: IPaymentMvvm.ViewModel) : WebViewClient() {
        init {
            viewModel.setProgress(true)
        }

        override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {

            view?.loadUrl(request?.url.toString())

            return true
        }

        override fun onPageFinished(view: WebView?, url: String?) {
            super.onPageFinished(view, url)
            viewModel.setProgress(false)
        }

        override fun onReceivedError(view: WebView?, request: WebResourceRequest?, error: WebResourceError?) {
            super.onReceivedError(view, request, error)
            viewModel.setProgress(false)
            Timber.e(error.toString())
        }
    }
}


class JSBridge(val context: Context, val activity: Activity) {
    @JavascriptInterface
    fun postMessage(message: String) {
        // here we return true if we handled the post.
        Timber.i(message)
        context.toast(message)

        val intent = Intent(context, OrderThanksActivity::class.java)
        intent.putExtra(Enums.contactDetail.contactNumber.name, activity.intent.getStringExtra(Enums.contactDetail.contactNumber.name))
        intent.putExtra(Enums.contactDetail.phoneNumber.name, activity.intent.getStringExtra(Enums.contactDetail.phoneNumber.name))
        intent.putExtra(Enums.contactDetail.cellNumber.name, activity.intent.getStringExtra(Enums.contactDetail.cellNumber.name))
        activity.startActivity(intent)
        activity.finish()
    }
}

1 个答案:

答案 0 :(得分:0)

这是来自Web的JavaScript脚本的问题,它甚至在调用我的界面函数之前就在Web视图中运行的脚本中引发了错误,但是奇怪的行为是该脚本仅在Android 5,6上被终止,而Android 7 Web视图不会终止执行并执行有关错误的整个脚本,这就是为什么在Android 7中调用接口函数的原因。