android 与html5交互之核心

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_30519365/article/details/54571411
package com.netease.nimlib.jsbridge.util;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.webkit.CookieManager;
import android.webkit.WebSettings;
import android.webkit.WebView;

/**
 * WebView 默认配置及适配
 * <p>
 * Created by hzsunyj on 15/12/3.
 */
public class WebViewConfig {

    public static final void setWebSettings(Context context, WebSettings settings, String cachePath) {
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setLoadWithOverviewMode(true);
        settings.setUseWideViewPort(true);
        settings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        settings.setAppCacheMaxSize(1024 * 1024 * 8);
        if (!TextUtils.isEmpty(cachePath)) {
            settings.setAppCachePath(cachePath);
        }
        settings.setAllowFileAccess(true);
        settings.setAppCacheEnabled(true);
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        settings.setSupportZoom(true);
        setDisplayZoomControls(settings);
        settings.setGeolocationEnabled(true);
        settings.setDatabaseEnabled(true);
        settings.setDefaultTextEncodingName("UTF-8");
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
    }

    private final static void setDisplayZoomControls(WebSettings webSettings) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            webSettings.setDisplayZoomControls(false);
        } else {
            webSettings.setBuiltInZoomControls(true);
        }
    }

    public static final void setAcceptThirdPartyCookies(WebView webView) {
        //target 23 default false, so manual set true
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            CookieManager.getInstance().setAcceptThirdPartyCookies(webView, true);
        }
    }

    public static final void setWebViewAllowDebug(boolean allowDebug) {
        if (allowDebug && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WebView.setWebContentsDebuggingEnabled(true);
        }
    }

    public static final void removeJavascriptInterfaces(WebView webView) {
        if (Build.VERSION.SDK_INT >= 11 && Build.VERSION.SDK_INT < 17) {
            removeJavascriptInterfaces11(webView);
        }
    }

    @TargetApi(11)
    private static final void removeJavascriptInterfaces11(WebView webView) {
        try {
            webView.removeJavascriptInterface("searchBoxJavaBridge_");
            webView.removeJavascriptInterface("accessibility");
            webView.removeJavascriptInterface("accessibilityTraversal");
        } catch (Throwable tr) {
            tr.printStackTrace();
        }
    }

}

以上power 来自云信demo

package com.netease.nim.demo.login;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.netease.nim.demo.R;
import com.netease.nim.demo.base.BasePage;
import com.netease.nimlib.jsbridge.util.WebViewConfig;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Administrator on 2017/1/5.
 */
@SuppressLint("SetJavaScriptEnabled")
public class ZhuwuType extends BasePage {
    @InjectView(R.id.wv)
    WebView wv;
    private Context ctx;

    @Override
    protected int getStatusBarColor() {
        return 0;
    }
    @Override
    protected void initData() {
        ctx=this;
        //"http://m.sosoapp.cn/zhipin/sousuo/search.html?Android=gsc
        //wv.getSettings().setDomStorageEnabled(true);
         initwebview();
        //wv.loadDataWithBaseURL("http://m.sosoapp.cn/zhipin/sousuo/search.html?Android=gsc","","text/html","UTF-8","");

    }
    @SuppressLint("JavascriptInterface")
    private void initwebview() {
        WebSettings settings = wv.getSettings();
        removeAllCookie();
        WebViewConfig.setWebSettings(this, settings, this.getApplicationInfo().dataDir);
        // WebViewConfig.removeJavascriptInterfaces(wv);
        WebViewConfig.setWebViewAllowDebug(false);
        WebViewConfig.setAcceptThirdPartyCookies(wv);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        wv.setWebChromeClient(new WebChromeClient());
        wv.addJavascriptInterface(new JsInteration(),"android");
        wv.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        wv.loadUrl("http://m.sosoapp.cn/wode/wodejianli/qiuzhiyixiang/occupation.html?Android=gsc");
//        wv.addJavascriptInterface(new Object(){
//            public void href() {
//                  finish();
//            }
//        },"location");
    }
    private void removeAllCookie()
    {
        CookieSyncManager cookieSyncManager = CookieSyncManager.createInstance(wv.getContext());
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        cookieManager.removeSessionCookie();

        //String testcookie1 = cookieManager.getCookie(urlpath);

        cookieManager.removeAllCookie();
        cookieSyncManager.sync();

        //String testcookie2 = cookieManager.getCookie(urlpath);
    }
    class JsInteration {
        @JavascriptInterface
        public void zhiye_href(String type) {
            System.out.println("---js-->>>"+type);
            try {
                JSONObject jsonObject=new JSONObject(type);
                String keyword = jsonObject.getString("keyword");
                if (!TextUtils.isEmpty(keyword)){
                    Intent i=new Intent();
                    i.putExtra("zhiyetype",keyword);
                    setResult(499,i);
                    finish();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }


           }

    }
    @Override
    protected void inintView() {
        ButterKnife.inject(this);
    }
    @Override
    protected int getLayout() {
        return R.layout.webpage;
    }


}


猜你喜欢

转载自blog.csdn.net/qq_30519365/article/details/54571411
今日推荐