【问题标题】:Google Play in WebView is not redirectingWebView 中的 Google Play 未重定向
【发布时间】:2018-03-13 02:51:31
【问题描述】:

我有一个指向 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 网页进行重定向。

【问题讨论】:

  • 你能贴一张你得到的图片吗?
  • 它很小,用波兰语写的,但应该很清楚:Google Play in browser + "Open in Google Play app" 浮动在底部。
  • 为什么要在 webview 中渲染它然后打开 Play 商店应用?

标签: android webview google-play android-webview


【解决方案1】:

这样试试

  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)));
    }

【讨论】:

  • 你没有抓住重点。我知道我可以使用意图重定向到 Google Play,但我需要使用链接来完成。稍后我会有跟踪链接。
  • 如何使用链接?这真的很清楚,它会启动一个指向某个链接的意图。
【解决方案2】:
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 表示 它正在处理事件。

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-20
      • 1970-01-01
      • 1970-01-01
      • 2017-08-31
      • 2011-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多