MainActivity.java 14.1 KB
package xdycom.abao.myapplication;

import android.app.Application;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.ConsoleMessage;
import android.webkit.JavascriptInterface;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import xdycom.abao.myapplication.application.MyApplicaiton;
import xdycom.abao.myapplication.sdk.GetRtmp;
import xdycom.abao.myapplication.sdk.MessageType;

import static xdycom.abao.myapplication.sdk.MessageType.CLASS_INIT_SUCCESS;

/**
 * 3.28 目前获取能初始化成功,注意需要对传入进来的jso需要n进行护处理,
 * 遗漏:获取js返回值的时候接受方法有问题。。。
 * 现在坑爹的需要布局的情况得到了解决,下步开始封装了
 */
public class MainActivity extends AppCompatActivity {
    private WebView mWebView;
    private Button bt_getScorce,bt_initClass,bt_class,bt_text;
    private final static String URL="http://h5.test.3mang.com/web/";
    private boolean isFirst=false;
    private EditText et_text;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mWebView= (WebView) this.findViewById(R.id.webview);
        bt_getScorce= (Button) this.findViewById(R.id.bt_getScorce);
        bt_initClass= (Button) this.findViewById(R.id.bt_initClass);
        et_text= (EditText) this.findViewById(R.id.et_text);
        bt_class= (Button) this.findViewById(R.id.bt_class);
        bt_text= (Button) this.findViewById(R.id.bt_text);
        init();
    }
    public void init(){
        WebSettings webSettings=mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.addJavascriptInterface(new MyJs(),"xdyAndroid");
        mWebView.loadUrl(URL);
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onConsoleMessage(String message, int lineNumber, String sourceID) {
            Log.e("webview", message + " -- From line " + lineNumber + " of " + sourceID);
                String id="";
                String jsonParmp="";
                //需要解析
//                switch(message){
//                    case "class_init_success":
                if(!isFirst) {
//                    id = "joinClass";
//                    String temp = "{\n" +
//                            "    \"userName\": \"\",\n" +
//                            "    \"password\": \"\",\n" +
//                            "    \"hasCamera\": true,\n" +
//                            "    \"hasMicrophone\": true\n" +
//                            "}";
//                    jsonParmp = stringToJson(temp, true);
//                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//                        mWebView.evaluateJavascript("javascript:_native2js('" + id + "','" + jsonParmp + "')", new ValueCallback<String>() {
//                            @Override
//                            public void onReceiveValue(String value) {
//                                Toast.makeText(MainActivity.this, "回调调用js" +
//                                        "" + value, Toast.LENGTH_SHORT).show();
//                            }
//                        });
//                    } else {
//
//                        mWebView.loadUrl("javascript:imageVal('init')");
//                    }
//                        break;
//                }
                    isFirst=true;
                }

            }

            @Override
            public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
                return super.onConsoleMessage(consoleMessage);
            }
        });
        bt_getScorce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

////                (1).ios/android调用: _native2js("接口名称","参数为JOSN字符串")
////                (2).sdk发送消息给ios/android: _js2native("事件消息名称","参数为JOSN字符串")
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//                    mWebView.evaluateJavascript("javascript:_native2js('" + interfaceName + ",'" + content + "')", new ValueCallback<String>() {
//                        @Override
//                        public void onReceiveValue(String value) {
//                            Toast.makeText(MainActivity.this,"回调调用js" +
//                                            ""+value, Toast.LENGTH_SHORT).show();
//
//                        }
//                    });
//                } else {
//                  mWebView.loadUrl("javascript:_native2js('" + interfaceName + ",'" + content + "')");
//                }

                GetRtmp getRtmp=new GetRtmp(MainActivity.this);
//                String jsonParmp="{\n" +
//                        "    \"classId\": 527720984,\n" +
//                        "    \"portal\": \"127.0.0.1:80\",\n" +
//                        "    \"userRole\": \"normal\",\n" +
//                        "    \"userName\": \"\",\n" +
//                        "    \"userId\": 0\n" +
//                        "}"
//                        ;
//                String rtmpString=getRtmp._native2js("init",jsonParmp);
//                Log.e("rtmpString:",jsonParmp);
//                Toast.makeText(MainActivity.this,rtmpString+"",Toast.LENGTH_LONG).show();


                String joinClass="{\n" +
                        "    \"userName\": \"base\",\n" +
                        "    \"password\": \"123456\",\n" +
                        "    \"hasCamera\": false,\n" +
                        "    \"hasMicrophone\": false\n" +
                        "}";

//                String rtmpString_joinClass=getRtmp._native2js("class_init_success","");
//                Log.e("rtmpString_joinClass:",joinClass);
//                Toast.makeText(MainActivity.this,rtmpString_joinClass+"",Toast.LENGTH_SHORT).show();

                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=stringToJson(jsonParmps,true);
                Log.e("转化后的json:",""+jsonParmp);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    mWebView.evaluateJavascript("javascript:_native2js('" + id + "','"+jsonParmp+"')", new ValueCallback<String>() {
                        @Override
                        public void onReceiveValue(String value) {
                            Toast.makeText(MainActivity.this, "回调调用js" +
                                    "" + value, Toast.LENGTH_SHORT).show();
                        }
                    });
                }else{

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

        bt_initClass.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String id="";
                String jsonParmp="";
                id = "joinClass";
                String temp = "{\n" +
                        "      \"userName\":\"base\",\n" +
                        "       \"password\":\"123321\",\n" +
                        "       \"hasCamera\":false,\n" +
                        "       \"hasMicrophone\":false}";

//                id = "chat_receive_message";
//                String temp = "{\"fromNodeId\": 772410521,\n" +
//                        "           \"toNodeId\": 0,\n" +
//                        "           \"message\": \"Hello world!\",\n" +
//                        "           \"fromName\": \"MyName\",\n" +
//                        "           \"fromRole\": \"normal\"\n" +
//                        "}";
                jsonParmp = stringToJson(temp, true);

                GetRtmp getRtmp=new GetRtmp(MainActivity.this);
                getRtmp._native2js(id,jsonParmp);
                _native2js(id,jsonParmp);
//                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//                    mWebView.evaluateJavascript("javascript:_native2js('" + id + "','" + jsonParmp + "')", new ValueCallback<String>() {
//                        @Override
//                        public void onReceiveValue(String value) {
//                            Toast.makeText(MainActivity.this, "回调调用js" +
//                                    "" + value, Toast.LENGTH_SHORT).show();
//                        }
//                    });
//                } else {
//
//                    mWebView.loadUrl("javascript:imageVal('init')");
//                }
            }
        });
        bt_class.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               String id=  et_text.getText().toString();
//               String id=  "sendStartClass";
                if(TextUtils.isEmpty(id)){
                   id="video_play";
                }
                id="video_play";
                _native2js(id);
                _native2js(id,"");
//                String id;
//                id = "chat_receive_message";
////                id = "sendChatMsg";
//
//                String temp = "{\"fromNodeId\": 772410521,\n" +
//                        "           \"toNodeId\": 0,\n" +
//                        "           \"message\": \"Helloss world!\",\n" +
//                        "           \"fromName\": \"base\",\n" +
//                        "           \"fromRole\": \"normal\"\n" +
//                        "}";
////                String temp = "{  \"message\":\"Helloss world!\",\n" +
////                        "       \"to\":772410521}";
//                String jsonParmp;
//                jsonParmp = stringToJson(temp, true);
//                _native2js(id,jsonParmp);
            }
        });

        bt_text.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=stringToJson(jsonParmps,true);
                GetRtmp getRtmp=new GetRtmp(MainActivity.this);
                getRtmp._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 static String stringToJson(String s,boolean isJSjson){
        StringBuffer sb = new StringBuffer();
        for(int i=0; i<s.length(); i++){
            char c =s.charAt(i);
            switch(c){
                case'\'': if(isJSjson) {sb.append("\\\'");}else{sb.append("\'");} break;
                case'\"': if(!isJSjson) {sb.append("\\\"");}else{sb.append("\"");} break;
                case'\\':sb.append("\\\\"); break; //如果不处理单引号,可以释放此段代码,若结合StringDanYinToJSON()处理单引号就必须注释掉该段代码
                case'/': sb.append("\\/");break;
                case'\b':sb.append("\\b");break;//退格
                case'\f':sb.append("\\f");break;//走纸换页
                case'\n':sb.append("\\n");break;//换行
                case'\r':sb.append("\\r");break;//回车
                case'\t':sb.append("\\t");break;//横向跳格
                default: sb.append(c);
            }}
        return sb.toString();
    }



    /**
     * 获取推流地址
     * @return
     */
    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) {
                mWebView.evaluateJavascript("javascript:_native2js('" + id + "','" + parmp + "')", new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String value) {
                        Toast.makeText(MainActivity.this, "回调调用js" +
                                "" + value, Toast.LENGTH_SHORT).show();
                    }
                });
            } else {

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

        }
        return "";
    }

    /**
     * 获取推流地址 一个参数
     * @return
     */
    public  String _native2js(  String id) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                mWebView.evaluateJavascript("javascript:_native2js('" + id + "')", new ValueCallback<String>() {
                    @Override
                    public void onReceiveValue(String value) {
                        Toast.makeText(MainActivity.this, "回调调用js" +
                                "" + value, Toast.LENGTH_SHORT).show();
                    }
                });
            } else {

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

        }
        return "";
    }
}