WebView中的Google Play未重定向

时间:2018-03-12 12:37:16

标签: android webview google-play android-webview

我有链接到Google Play,可以打开Chrome并自动重定向到Android上的Play应用程序:

https://play.google.com/store/apps/details?id=com.raongames.growcastle

我想通过在隐藏的WebView上呈现它来直接重定向到Play应用程序:

private fun openUrlForRedirection(linkToOffer: String) {
    loading = true
    webView.apply {
        setWebChromeClient(RedirectWebChromeClient(context))
        setWebViewClient(RedirectWebViewClient(::onRedirect, ::onRedirectionError))
        getSettings().apply {
            javaScriptEnabled = true
            javaScriptCanOpenWindowsAutomatically = true
            userAgentString = System.getProperty("http.agent")
            loadUrl(linkToOffer)
        }
    }
}

private fun onRedirect(str: String) {
    loading = false
    if (quest.pkg !in str) {
        installApp(quest.pkg)
    } else {
        openUrl(str)
    }
}

private fun onRedirectionError() {
    showAndLogError(UnknownError())
}

class RedirectWebChromeClient(val context: Context) : WebChromeClient() {

    override fun onJsAlert(webView: WebView, str: String, str2: String, jsResult: JsResult): Boolean {
        jsResult.cancel()
        return true
    }

    override fun onJsConfirm(webView: WebView, str: String, str2: String, jsResult: JsResult): Boolean {
        jsResult.cancel()
        return true
    }

    override fun onJsPrompt(webView: WebView, str: String, str2: String, str3: String, jsPromptResult: JsPromptResult): Boolean {
        jsPromptResult.cancel()
        return true
    }
}

class RedirectWebViewClient(
        private val onRedirect: (String) -> Unit,
        private val onError: () -> Unit
) : WebViewClient() {

    override fun onPageFinished(view: WebView, url: String) {
        info("Finished")
    }

    override fun onReceivedError(webView: WebView, i: Int, str: String, str2: String) {
        if (i != -10) {
            onError()
        }
    }

    @TargetApi(23)
    override fun onReceivedError(webView: WebView, webResourceRequest: WebResourceRequest, webResourceError: WebResourceError) {
        onReceivedError(webView, webResourceError.errorCode, webResourceError.description.toString(), webResourceRequest.url.toString())
    }

    @RequiresApi(api = 21)
    override fun shouldOverrideUrlLoading(webView: WebView, webResourceRequest: WebResourceRequest): Boolean {
        return shouldOverrideUrlLoading(webView, webResourceRequest.url.toString())
    }

    override fun shouldOverrideUrlLoading(webView: WebView, str: String): Boolean {
        onRedirect(str)
        return false
    }
}

但是,我没有重定向,而是使用带有按钮的Google Play网页进行重定向。

3 个答案:

答案 0 :(得分:0)

试试这个

  final String appPackageName = activity.getPackageName(); // getPackageName() from Context or Activity object
    try {
    //if you have playstore it's will navigate to playstore
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
    } catch (android.content.ActivityNotFoundException anfe) {
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName)));
    }

答案 1 :(得分:0)

browser=(WebView)findViewById(R.id.webkit);

browser.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
// here do what you need with url
//for example use Intent to start google play 
//or simply call view.loadUrl(url); to prevent chrome from opening

         return(true);
         }
            });

shouldOverrideUrlLoading()返回true表示 它正在处理这个事件。

答案 2 :(得分:0)

不建议通过网页浏览,甚至可能在将来某个时候停止工作。通过意图绝对是推荐的解决方案,并为用户提供更好的体验。

如果您需要带有跟踪的链接,Google Play允许使用Firebase analytics通过Play应用跟踪安装。