whyclj
2019-07-24 c9f8b9e09489e547734246140b4fd3635100fbb6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package com.activitys;
 
import androidx.annotation.NonNull;
 
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.ViewGroup;
import android.webkit.JavascriptInterface;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
 
import com.comm.ServiceModel;
import com.example.webapp.R;
import com.fbo2206.FBO_ParamDischarge;
import com.fbo2206.MySocketClientThread;
import com.fbs9100.FBS_ComBase;
import com.google.gson.Gson;
 
import io.dcloud.c.h;
 
public class Index_activity extends Activity {
    private final static  String TAG = "Index_activity";
    private ServiceModel model;
    private WebView webView;
    private Handler handler;
 
    private MyJavaScriptInterface myJavaScriptInterface;
 
    private MySocketClientThread socketClientThread = null;
 
 
    @SuppressLint("JavascriptInterface")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.index_layout);
 
        //方式1:直接在在Activity中生成
        webView = (WebView)findViewById(R.id.my_webview);
        //handler = new Handler();
 
        //设置支持JavaScript语言,让WebView支持javascript的动作
        webView.getSettings().setJavaScriptEnabled(true);
 
        WebSettings webSettings = webView.getSettings();
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setDefaultTextEncodingName("utf-8");
 
        //交互对象的添加,传入对象在html5界面的javascript中获取,该对象用于两者之间的交互。question,给JavaScript添加一个与之交互的接口,传入一个对象和该对象的名字,在html5端根据这个名字得到对象
        //提供一个供h5交互的对象,并给对象标记个名字供js识别
        //webView.addJavascriptInterface( model = new ServiceModel(), "model");
        //webView.addJavascriptInterface( mUser = new User(), "user");
        webView.addJavascriptInterface( myJavaScriptInterface = new MyJavaScriptInterface(), "JavaScriptInterface");
        //自定义实现alert
        webView.setWebChromeClient(new MyWebChromClient());
        //要加载的页面
        webView.loadUrl("file:///android_asset/apps/HelloH5/www/index.html");
 
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view,String  url) {
                view.loadUrl(url);
                return true;
            }
 
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }
        });
 
        handler = new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
            if(webView != null){
                ServiceModel model = (ServiceModel)msg.obj;
                Log.e(TAG, "handleMessage: "+model+"%%%%%%%%%%%%" );
                myJavaScriptInterface.sendMessageToJavaScript(model);
            }
            }
        };
 
        socketClientThread = new MySocketClientThread();
        socketClientThread.start();
    }
 
    @Override
    protected void onDestroy() {
        if (webView != null) {
            webView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            webView.clearHistory();
 
            ((ViewGroup) webView.getParent()).removeView(webView);
            webView.destroy();
            webView = null;
        }
        super.onDestroy();
    }
 
 
    class MyWebViewClient extends WebViewClient {
        //不重新打开系统的浏览器加载网页
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
 
    }
    private class MyWebChromClient extends WebChromeClient{
        //捕获html5 js中的alert事件, 将alert事件转换为Toast形式显示,但是不道为啥我的能toast,但是toast之后输入框的焦点就没有了,不能再输入第二次了,我用的是魅族手机
        @Override
        public boolean onJsAlert(WebView view, String url, String message,
                        JsResult result) {
            Toast.makeText(Index_activity.this, message, Toast.LENGTH_SHORT).show();
            Log.i("TAG", message);
            return true;
        }
 
    }
 
    class MyJavaScriptInterface{
        private Gson gson ;
 
        //将接收到的数据发送给前台JS
        public void sendMessageToJavaScript(ServiceModel model){
            gson = new Gson();
            String json = gson.toJson(model);
            webView.loadUrl("javascript:showInfoFromJava('" + json + "')");
        }
 
        @JavascriptInterface
        //接收前台JS发送的数据
        public void receiveMessageFromJavaScript(String json){
            Log.e(TAG, "receiveMessageFromJavaScript: $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"+json);
            gson = new Gson();
            model = gson.fromJson(json,ServiceModel.class);
            Log.e(TAG, "receiveMessageFromJavaScript: "+model);
            Log.e(TAG, "receiveMessageFromJavaScript: "+"##############################################################" );
            Object obj = null;
            if(FBS_ComBase.CMD_SETDEVICEIP == model.cmd){
                //设置设备的ip地址
                MySocketClientThread.server_ip = model.msg;
                model.cmd = FBS_ComBase.CMD_STARTCNNECT;                //开始通信
 
            }else if(FBS_ComBase.CMD_SetDischargeParm == model.cmd){
                FBO_ParamDischarge param = gson.fromJson(model.data.toString(),FBO_ParamDischarge.class);
                obj = param;
            }
 
            sendMessageToSocketClient(handler,model.cmd,obj);
        }
    }
 
    private boolean sendMessageToSocketClient(Handler handler, int cmd, Object FBS_data)
    {
        ServiceModel model = new ServiceModel();
        model.handler = handler;
        model.msg = "Request FBSdata from BattChargeParamACT";
        model.cmd = cmd;
        model.data = FBS_data;
 
        Message childMsg = socketClientThread.mHandler.obtainMessage();
        childMsg.obj = model;
        return (socketClientThread.mHandler.sendMessage(childMsg));
    }
}