有webView

时间:2018-01-21 14:45:20

标签: android android-studio webview kotlin android-webview

我有一个包含按钮的活动,该按钮启动打开另一个活动的意图并设置一些额外的数据,例如:

val intent = Intent(this, ViewOnline::class.java)
intent.putExtra(URL_TO_SEND_KEY, url)
this.startActivity(intent)

在启动的活动中,它只包含一个webView而不包含任何其他内容。在onCreate我有:

webView.webViewClient = webViewClient

webView.settings.javaScriptEnabled = true
webView.settings.loadWithOverviewMode = true
webView.settings.useWideViewPort = true

val url = intent.extras.get(URL_TO_SEND_KEY) as String?
if (url != null) {
    webView.loadUrl(url)
}

这一切都很好,但是,当按下按钮启动活动时,在显示新活动之前大约需要5秒钟,我无法弄清楚它正在做什么以及为什么需要这么长时间。有什么想法吗?

更新:它似乎只是模拟器中的一个问题。真实设备似乎没有遇到同样的问题。

以下是完整的课程:

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.os.Build
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.webkit.WebView
import android.webkit.WebViewClient
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.MobileAds
import kotlinx.android.synthetic.main.resources_online.*
import org.jetbrains.anko.indeterminateProgressDialog

@SuppressLint("SetJavaScriptEnabled")
class ViewResourcesOnline : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(R.layout.resources_online)

        setupAdView()

        loadBannerAd()

        val loadingDialogue = indeterminateProgressDialog("Loading...")

        val webViewClient: WebViewClient = object: WebViewClient() {
            override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)
                loadingDialogue.dismiss()
            }

            override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                super.onPageStarted(view, url, favicon)
                loadingDialogue.show()
            }
        }

        button_back_resources_online.setOnClickListener {
            finish()
        }

        if (Build.VERSION.SDK_INT >= 19) {
            webView_resources_online.setLayerType(View.LAYER_TYPE_HARDWARE, null)
        }
        else {
            webView_resources_online.setLayerType(View.LAYER_TYPE_SOFTWARE, null)
        }

        webView_resources_online.webViewClient = webViewClient

        webView_resources_online.settings.javaScriptEnabled = true
        webView_resources_online.settings.loadWithOverviewMode = true
        webView_resources_online.settings.useWideViewPort = true

        val url = intent.extras.get(RESOURCES_KEY) as String?

        when {
            (url != null) -> {
                webView_resources_online.loadUrl(url)
            }
            else -> {
                finish()
            }
        }
    }

    private fun setupAdView() {
        adView_resources_online.adListener = object: AdListener() {
            override fun onAdLoaded() {
                adView_resources_online.visibility = View.VISIBLE
            }

            override fun onAdFailedToLoad(errorCode : Int) {
                adView_resources_online.visibility = View.GONE
            }

        }
    }

    private fun loadBannerAd() {
        MobileAds.initialize(this, getString(R.string.adMobAppId))

        if (!inAppPurchase) {
            adView_resources_online.loadAd(AdRequest.Builder().build())
        }
    }
}

0 个答案:

没有答案