MyWebView.java 2.0 KB
package xdycom.abao.myapplication;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

/**
 * Created by abao on 2017/3/30.
 */

public class MyWebView extends WebView {
    private final static String URL="http://h5.test.3mang.com/web/";
    public MyWebView(Context context) {
        super(context);
//        myWebView=new MyWebView(context);
//        WebSettings webSettings=myWebView.getSettings();
//        webSettings.setJavaScriptEnabled(true);
//        myWebView.addJavascriptInterface(new MyJs(),"xdyAndroid");
//
//        myWebView.loadUrl(URL);
    }

    public MyWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    class MyJs  {
        @JavascriptInterface
        public String _js2native(String id,String parmp){
          /*  _native2js("接口名称","参数为JOSN字符串")*/
            Log.e("js2native","id:"+id+"  parmp:"+parmp);
            return parmp;
        }

        @JavascriptInterface
        public String _js2native(String parmp){
          /*  _native2js("接口名称","参数为JOSN字符串")*/
            Log.e("js2native", " parmp:"+parmp);
            return parmp;
        }
    }

    /**
     * 获取推流地址
     * @return
     */
    public  String _native2js(  String id, String parmp) {
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//                myWebView.evaluateJavascript("javascript:_native2js('" + id + "','" + parmp + "')", new ValueCallback<String>() {
//                    @Override
//                    public void onReceiveValue(String value) {
//                      Log.e("MSG_evaluate","_native2js"+value);
//                    }
//                });
//            } else {
//
////                mWebView.loadUrl("javascript:imageVal('init')");
//            }
        return "";
    }
}