SecondActivity.java 3.4 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.View;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
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.view.WebViewBase;

public class SecondActivity extends AppCompatActivity {
    WebView webViewBase;
    private final static String URL="http://h5.test.3mang.com/web/";
    private Button bt_class;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        bt_class= (Button) this.findViewById(R.id.bt_class);
        bt_class.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                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);
                Log.e("转化后的json:",""+jsonParmp);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    webViewBase.evaluateJavascript("javascript:_native2js('" + id + "','"+jsonParmp+"')", new ValueCallback<String>() {
                        @Override
                        public void onReceiveValue(String value) {
                            Toast.makeText(SecondActivity.this, "回调调用js" +
                                    "" + value, Toast.LENGTH_SHORT).show();
                        }
                    });
                }else{

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


        init();
    }
    public void init(){
        webViewBase=new WebView(MyApplicaiton.getInstance());
        WebSettings webSettings=webViewBase.getSettings();
        webSettings.setJavaScriptEnabled(true);
//        webViewBase.setWebViewClient(new WebViewClient(){
//            @Override
//            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
//                return super.shouldOverrideUrlLoading(view, request);
//            }
//        });
        webViewBase.addJavascriptInterface(new MyJs(),"xdyAndroid");
        webViewBase.loadUrl(URL);
    }


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

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