URL在WebView中的自动重定向 – Android

huangapple go评论75阅读模式
英文:

URL Automatic redirecting in WebView - Android

问题

以下是翻译好的内容:

在 XML 中:

<WebView
    android:layout_alignParentTop="true"
    android:id="@+id/campaign_webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

在 Activity 中:

campaign_webview.getSettings().setJavaScriptEnabled(true);
campaign_webview.getSettings().setLoadWithOverviewMode(true);
campaign_webview.getSettings().setDomStorageEnabled(true);
campaign_webview.getSettings().setUseWideViewPort(true);
campaign_webview.getSettings().setBuiltInZoomControls(false);
campaign_webview.getSettings().setPluginState(WebSettings.PluginState.ON);
campaign_webview.setWebViewClient(new CustomClient());
campaign_webview.loadUrl(url);

private class CustomClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return false;
    }

    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        handler.proceed();
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        Log.d("web_view: finish", url);
    }
}
英文:

My WebView in my app are working fine, but when i load the URL= https://www.esheba.cnsbd.com/#/
Its automatically Redirecting to a Play store Link which is linked as download link in bottom of the website, But when i brows the link form browser is working fine. Please anyone tell me why its redirecting only in WebView ?
here is my codes are using.

In XML

&lt;WebView
        android:layout_alignParentTop=&quot;true&quot;
        android:id=&quot;@+id/campaign_webview&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;/&gt;

In Activity

campaign_webview.getSettings().setJavaScriptEnabled(true);
campaign_webview.getSettings().setLoadWithOverviewMode(true);
campaign_webview.getSettings().setDomStorageEnabled(true);
campaign_webview.getSettings().setUseWideViewPort(true);
campaign_webview.getSettings().setBuiltInZoomControls(false);
campaign_webview.getSettings().setPluginState(WebSettings.PluginState.ON);
campaign_webview.setWebViewClient( new CustomClient());
campaign_webview.loadUrl(url);

private class CustomClient extends WebViewClient{
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return false;
        }

        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            handler.proceed();
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            Log.d(&quot;web_view: finish&quot;,url);
        }

    }

答案1

得分: 1

我认为您想在链接为Play Store链接时进行重定向到Play Store。所以,如果这是您想要的内容,您可以使用以下代码:

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.startsWith("intent://")) {
        //加载商店
        try {
            Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
            PackageManager packageManager = context.getPackageManager();
            ResolveInfo info = packageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
            if (info != null) {
                context.startActivity(intent);
            }
            return true;
        } catch (ActivityNotFoundException e) {
            //如果未找到活动,请尝试加载它并显示一个toast。
            Toast.makeText(context, "Activity not found", Toast.LENGTH_SHORT).show();
            return false;
        }
    } else {
        //加载URL
        view.loadUrl(url);
        return false;
    }
}
英文:

I think you want to redirect to Play Store if it is a Play Store link. So if that is what you want then you can use:

@Override
    public boolean shouldOverrideUrlLoading(WebView 
    view, String url) {
        
    if (url.startsWith(&quot;intent://&quot;)) {
    //load store
         try{
         Intent intent = Intent.parseUri(url, 
           Intent.URI_INTENT_SCHEME);
         PackageManager packageManager = 
         context.getPackageManager();
         ResolveInfo info = 
          packageManager.resolveActivity(intent, 
           PackageManager.MATCH_DEFAULT_ONLY);
       if (info != null) {
       context.startActivity(intent);
        }

         return true;

       }catch((ActivityNotFoundException e)
       {
          //If activity not found try to load it and also a display toast.


          Toast.makeText(context, &quot;Activity not found&quot;, Toast.LENGTH_SHORT).show();

          return false;
       }
   
   }
   else
   {
          //load url
          view.loadUrl(url);
           return false;
    }

}

huangapple
  • 本文由 发表于 2020年8月31日 00:55:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63659875.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定