码迷,mamicode.com
首页 > Web开发 > 详细

百川sdk----自己的WebViewClient不被执行

时间:2016-06-07 14:34:15      阅读:362      评论:0      收藏:0      [点我收藏+]

标签:

我在百川sdk的旺旺群中,追问这个问题N多次,一直没有人答复,哎,凡事都要靠自己.....

1、先查看下百川sdk中,是怎么处理咱们传递过去的 WebViewClient 

public class l implements WebViewService {
    private static final String b = l.class.getSimpleName();
    private static final String c;
    private static final String d;
    private String e;
    public static boolean a;
    private String f = "";
    private String g;
    private HashMap<String, String> h = new HashMap();

    public l() {
    }

    public void bindWebView(WebView var1, WebViewClient var2) {
        if(var1 != null) {
            a = false;
            WebSettings var5 = var1.getSettings();

            try {
                var5.setJavaScriptEnabled(true);
            } catch (Exception var7) {
                ;
            }

            var5.setSavePassword(false);
            var5.setUseWideViewPort(true);
            var5.setLoadWithOverviewMode(true);
            var5.setJavaScriptCanOpenWindowsAutomatically(false);
            var5.setDomStorageEnabled(true);
            this.g = var1.getContext().getApplicationContext().getDir("cache", 0).getPath();
            var5.setAppCachePath(this.g);
            var5.setAllowFileAccess(true);
            var5.setAppCacheEnabled(true);
            if(CommonUtils.isNetworkAvailable(var1.getContext())) {
                var5.setCacheMode(-1);
            } else {
                var5.setCacheMode(1);
            }

            var5.setBuiltInZoomControls(false);
            StringBuilder var6 = new StringBuilder();
            this.e = var5.getUserAgentString();
            if(this.e != null) {
                var6.append(this.e);
            }

            if(!WebViewUtils.isLoginDowngraded()) {
                var6.append(d);
            }

            var6.append(c);
            var5.setUserAgentString(var6.toString());
            if(VERSION.SDK_INT >= 21) {
                CookieManager.getInstance().setAcceptThirdPartyCookies(var1, true);
                int var10000 = d.e == null?-1:d.e.getIntValue("mixedContentMode", -1);
                int var8 = var10000;
                if(var10000 != -1) {
                    var5.setMixedContentMode(var8);
                }
            }

            if(var2 == null) {
                var1.setWebViewClient(new m(this));
            } else {
                var1.setWebViewClient(new n(this));
            }
        }
    }

    public void releaseWebView(WebView var1) {
        var1.getSettings().setUserAgentString(this.e);
        d.m.removeCookies();
    }

    protected static void a(String var0) {
        if(!WebViewUtils.isLoginDowngraded()) {
            try {
                CookieManagerWrapper.INSTANCE.refreshCookie(var0);
            } catch (Exception var1) {
                AliSDKLogger.e("ui", "fail to refresh cookie", var1);
            }
        }
    }

    static {
        c = " AliApp(BC/" + ConfigManager.TAE_SDK_VERSION.toString() + ")";
        d = " tae_sdk_" + ConfigManager.SDK_INTERNAL_VERSION;
        a = false;
    }
}

关键在 bindWebView(WebView var1, WebViewClient var2) 方法中 var2 做了些什么!!...

if(var2 == null) {
    var1.setWebViewClient(new m(this));
} else {
    var1.setWebViewClient(new n(this));
}

哇擦擦,只是用来判断,连基本的保存都没有。可想我的 WebViewClient  死的多惨。

既然百川不带咱们的 WebViewClient  玩,那就想想其他办法,它不带咱们玩,那就咱们带它玩吧....

那怎么玩呢?咱们的 WebViewClient 来包裹百川的 WebViewClient ,然后在通过 webView.setWebViewClient 方法把新的 WebViewClient  重新设置进去。  

具体实现如下

1、首先绑定百川的 WebView 服务

mWebViewService = AlibabaSDK.getService(WebViewService.class);
mWebViewService.bindWebView(webView,  null);

2、通过反射从系统的 WebView 获得百川的 WebViewClient 

/** 获得隐藏成员变量mProvider中的WebViewClient */
public WebViewClient getProviderWebViewClient() {
    WebViewClient webViewClient = null;
    try {
        Class<?> cls = this.getClass();
        Method method = cls.getMethod("getWebViewProvider");
        method.setAccessible(true);
        Object object = method.invoke(this); // object => WebViewChromium implements WebViewProvider
        Field field = object.getClass().getDeclaredField("mContentsClientAdapter");
        field.setAccessible(true);
        object= field.get(object); // object => WebViewContentsClientAdapter
        field = object.getClass().getDeclaredField("mWebViewClient");
        field.setAccessible(true);
        object= field.get(object);
        if (object instanceof WebViewClient) { webViewClient = (WebViewClient) object; }
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } finally { return webViewClient; }
}

3、将自己的WebViewClient 和百川的WebViewClient 结合

package com.emar.bcsdk;

import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
 * Created by ak_star on 2016/6/2.
 */
public class MixBaiCWebViewClient extends WebViewClient {
    private WebViewClient mMyselfClient = null; // 自己应用的WebViewClient
    private WebViewClient mBaiCClient = null; // 百川WebViewClient

    public MixBaiCWebViewClient(WebViewClient myself, WebViewClient baiClient) {
        mMyselfClient = myself;
        mBaiCClient = baiClient;
    }

    @Override
    public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
        super.onReceivedError(view, errorCode, description, failingUrl);
        if (mBaiCClient != null) { mBaiCClient.onReceivedError(view, errorCode, description, failingUrl); }
        if (mMyselfClient != null) { mMyselfClient.onReceivedError(view, errorCode, description, failingUrl); }
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        if (mBaiCClient != null) { mBaiCClient.onPageStarted(view, url, favicon); }
        if (mMyselfClient != null) { mMyselfClient.onPageStarted(view, url, favicon); }
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        if (mBaiCClient != null) { mBaiCClient.onPageFinished(view, url); }
        if (mMyselfClient != null) { mMyselfClient.onPageFinished(view, url); }
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        boolean result = super.shouldOverrideUrlLoading(view, url);
        if (mMyselfClient != null)
            result = mMyselfClient.shouldOverrideUrlLoading(view, url);
        if (mBaiCClient != null) {
            if (!result) {
                result = mBaiCClient.shouldOverrideUrlLoading(view, url);
            } else { mBaiCClient.shouldOverrideUrlLoading(view, url); }
        }
        return result;
    }

    @TargetApi(14)
    @Override
    public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
        super.onReceivedSslError(view, handler, error);
        if (mBaiCClient != null) {
            mBaiCClient.onReceivedSslError(view, handler, error);
        } else if (mMyselfClient != null) {
            mMyselfClient.onReceivedSslError(view, handler, error);
        }
    }
}

4、最后重新设置给 WebView 

webView.setWebViewClient(new MixBaiCWebViewClient(client, baiCWebViewClient));

 

上述方案已经经过本人测试,可以实现 淘宝授权免登服务,同时也触发了自己的 WebViewClient 中的方法执行。

但毕竟此方案不是正规途径,有正规方法,还是用正规方法。

注意:1、此方法再升级百川sdk后,要重新检查是否仍然可行

        2、由于使用了反射,android sdk 如果变量名称、类型改变也可能失效,需要重新确定反射对象。  

     

    

            

百川sdk----自己的WebViewClient不被执行

标签:

原文地址:http://www.cnblogs.com/rabbit-ak-star/p/5566638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!