LunchActivity.java 6.3 KB
package xdycom.abao.myapplication;

import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;

import xdycom.abao.myapplication.application.MyApplicaiton;
import xdycom.abao.myapplication.utisl.StringUtils;
import xdycom.abao.myapplication.utisl.XdyWebView;

public class LunchActivity extends AppCompatActivity implements View.OnClickListener{
    private Button bt_start;
    private MyWebView myWebView;
    WebView webView;
    private final static String URL="http://h5.test.3mang.com/web/";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lunch);
        bt_start= (Button) this.findViewById(R.id.bt_start);
        bt_start.setOnClickListener(this);
        init();
//        initData();
    }
    public void init (){
        bt_start= (Button) this.findViewById(R.id.bt_start);
        bt_start.setOnClickListener(this);
//        myWebView= (MyWebView) this.findViewById(R.id.mywebview);

    }

    public void initData(){
        myWebView=new MyWebView(this);
        myWebView.setWebChromeClient(new WebChromeClient(){
        });
        myWebView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                myWebView.loadUrl(URL);
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                super.onReceivedError(view, errorCode, description, failingUrl);
                Log.e("webview","errorCode"+errorCode);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_start:
//                initClass();
//                initClass();
                inidwebView();
                break;

        }
    }

    /**
     * 初始化课堂
     */
    public void initClass(){
       View view= LayoutInflater.from(this).inflate(R.layout.activity_main,null);
         webView= (WebView) view.findViewById(R.id.webview);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
            }

        });
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onConsoleMessage(String message, int lineNumber, String sourceID) {
                super.onConsoleMessage(message, lineNumber, sourceID);
                Log.e(null,"Message:"+message+" lineNumber:");
            }
        });
       webView.getSettings().setJavaScriptEnabled(true);
        webView.addJavascriptInterface(new MyJs(),"xdyAndroid" );
        webView.loadUrl(URL);
        String id="init";
        String jsonParmps="{\n" +
                "    \"classId\": 527720984,\n" +
                "    \"portal\": \"112.126.80.182:90\",\n" +
                "    \"userRole\": \"normal\",\n" +
                "    \"userName\": \"\",\n" +
                "    \"userId\": 0\n" +
                "}";
        String jsonParmp= StringUtils.stringToJson(jsonParmps,true);
         _native2js(id,jsonParmp);
    }

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

    public  String _native2js(  String id, String parmp) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                webView.evaluateJavascript("javascript:_native2js('" + id + "','" + parmp + "')", new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String value) {
                   Log.e("显示:界面","啊哈");
                    }
                });
            } else {

//                mWebView.loadUrl("javascript:imageVal('init')");
            }

        }
        return "";
    }


    public void inidwebView(){
        WebView xdyWebView=new WebView(this);
        xdyWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);
            }
        });
        xdyWebView.getSettings().setJavaScriptEnabled(true);//支持Html5标签
        xdyWebView.getSettings().setDomStorageEnabled(true);//支持Html5标签
        xdyWebView.addJavascriptInterface(new MyJs(),"xdyAndroid");
        xdyWebView.loadUrl(URL);
        String id="init";
        String jsonParmps="{\n" +
                "    \"classId\": 527720984,\n" +
                "    \"portal\": \"112.126.80.182:90\",\n" +
                "    \"userRole\": \"normal\",\n" +
                "    \"userName\": \"\",\n" +
                "    \"userId\": 0\n" +
                "}";
        String jsonParmp= StringUtils.stringToJson(jsonParmps,true);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            xdyWebView.evaluateJavascript("javascript:_native2js('" + id + "','" + jsonParmp + "')", new ValueCallback<String>() {
                @Override
                public void onReceiveValue(String value) {
                    Log.e("显示:界面","啊哈");
                }
            });
        } else {

            xdyWebView.loadUrl("javascript:imageVal('init')");
        }

    }
}