whyclj
2019-10-21 aecc5aa11fd2cbdea193db53adfc7c7920f43b26
修复bug
5个文件已修改
1个文件已添加
150 ■■■■■ 已修改文件
platforms/android/app/src/main/java/com/mode/BattTestParam.java 3 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
platforms/android/app/src/main/java/com/service/MyInteractionService.java 45 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
platforms/android/app/src/main/java/com/socket/MySocketClientThread.java 52 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
platforms/android/app/src/main/java/io/cordova/hellocordova/CheckExitService.java 45 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
platforms/android/app/src/main/java/io/cordova/hellocordova/MainActivity.java 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
platforms/android/app/src/main/java/io/hybird/jsbridge/JsInterface.java 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
platforms/android/app/src/main/java/com/mode/BattTestParam.java
@@ -1,5 +1,7 @@
package com.mode;
import android.util.Log;
import com.util.FBS_ComBase;
import java.io.UnsupportedEncodingException;
@@ -53,6 +55,7 @@
    public boolean putByteBuffer(ByteBuffer bf) {
        if(bf.limit()<BYTE_LEN) {
            Log.e("BattTestParam", bf.limit()+"putByteBuffer: "+BYTE_LEN);
            return false;
        }
        ByteBuffer tmpbuf = bf;
platforms/android/app/src/main/java/com/service/MyInteractionService.java
@@ -29,11 +29,12 @@
    private DeviceState deviceState = null;
    private FTPFileUtil ftpFileUtil = null;
    private boolean isRunning = false;
    public MyInteractionService(String serverip, JsInterface jsInterface){
        sockethread = new MySocketClientThread(serverip);
        ftpFileUtil = new FTPFileUtil();
        sockethread.start();
        this.serverip = serverip;
        this.mSocketHandle = new Handler(){
@@ -44,9 +45,9 @@
                    //获取测试参数
                    jsInterface.SendCallDataToJS("getTestParam",model);
                }else if(FBS_ComBase.CMD_HEARTBEAT == model.cmd){
                    //心跳包返回
                    //心跳包返
                    deviceState = (DeviceState)model.data;
                    jsInterface.SendCallDataToJS("getNowWorkBatt",model);
                    //jsInterface.SendCallDataToJS("getNowWorkBatt",model);
                }else if(FBS_ComBase.CMD_CREATENEWBATT == model.cmd){
                    //新建电池组
                    //model.data = deviceState;
@@ -98,24 +99,26 @@
            }
        };
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    try {
                        if(isRunning){
                            //发送心跳包给设备
                            sendMessageToSocketClient(mSocketHandle, FBS_ComBase.CMD_HEARTBEAT, "");
                            Log.e(TAG, "run: ######################################" );
                        }
                        //handler.removeCallbacks(runnable);
                        Thread.sleep(4000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                while (true){
//                    try {
//                        //发送心跳包给设备
//                        sendMessageToSocketClient(mSocketHandle, FBS_ComBase.CMD_HEARTBEAT, "");
//                        Log.e(TAG, "run: ######################################" );
//                        //handler.removeCallbacks(runnable);
//                        Thread.sleep(1000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//        }).start();
        this.sockethread.start();
    }
    //设置设备ip
@@ -123,7 +126,9 @@
        boolean flag = true;
        this.serverip = serverip;
        this.ftpFileUtil.server_ip = serverip;
        flag = sockethread.setUpDeviceIp(serverip);
        isRunning = true;
        return flag;
    }
platforms/android/app/src/main/java/com/socket/MySocketClientThread.java
@@ -44,9 +44,10 @@
    private ByteBuffer bytebuffer_for_socket_RX = ByteBuffer.allocate(1500);
    public Handler mHandler = null;
    public static Socket socket;
    public static Socket socket = null;
    private Date lastConTime;                       //上一次通讯成功的时间
    private boolean thread_run_flag = true;
    public boolean isRuning = false;
    ServiceModel returnmodel = new ServiceModel();
@@ -55,12 +56,7 @@
        battData = new BattRTData();
        testParam = new BattTestParam();
        testRtData = new BattTestRtData();
        new Thread(new Runnable() {
            @Override
            public void run() {
                reConnectDevice();
            }
        }).start();
    }
    /*********************************************************************************************/
@@ -69,7 +65,7 @@
        //初始化消息循环队列,需要在Handler创建之前
        Looper.prepare();
        isRuning = true;
        mHandler = new Handler()
        {
            public void handleMessage(Message msg)
@@ -112,7 +108,15 @@
        Log.e(TAG, "Loop start at "+ Com.getDateTimeFormat(new Date(),Com.DTF_YMDhms) );
        //启动子线程消息循环队列
        Looper.loop();
        isRuning = false;
        Log.e(TAG, "Loop stop at "+ Com.getDateTimeFormat(new Date(),Com.DTF_YMDhms) );
        if(socket != null){
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /**
@@ -130,6 +134,10 @@
//            socket = new Socket();
//            SocketAddress socAddress = new InetSocketAddress(server_ip, server_port);
//            socket.connect(socAddress, 2000);
            if(socket == null || socket.isClosed()){
                Log.e(TAG, "SocketComm:!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! " );
                reConnectDevice();
            }
            if(this.socket != null)
            {
                in = this.socket.getInputStream();
@@ -233,6 +241,7 @@
                    res_t = false;
                    model.code = 0;
                }
                thread_run_flag = true;
            } else {
                //System.out.println(this.getName() + "-------socket  异常关闭-------" + Com.getNowTimeWithAt());
                thread_run_flag = false;
@@ -240,20 +249,6 @@
        } catch (IOException | InterruptedException e) {
            thread_run_flag = false;
        } finally {
            if(in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(!thread_run_flag){
                reConnectDevice();
            }
@@ -387,8 +382,9 @@
            }
            //---------------------  获取放电参数    ----------------------------------------------
            else if(FBS_ComBase.CMD_GETDISCHARGEPARAM == m_FBS_Cmd.CMD) {
                Log.e(TAG, "getDataFromCommBuf: 获取放电参数" );
                if(testParam.putByteBuffer(bf)) {
                    //System.out.println("获取放电参数成功");
                    System.out.println("获取放电参数成功");
                    //System.out.println(testParam);
                    isSuccess = true;
                    model.setData(testParam);
@@ -483,9 +479,13 @@
    public boolean setUpDeviceIp(String server_ip){
        boolean flag  = false;
        synchronized (socket){
            if (server_ip != null && server_ip.length() > 0){
                flag = reConnectDevice();
        if(this.socket == null){
            flag = reConnectDevice();
        }else{
            synchronized (socket){
                if (this.server_ip != null && server_ip.length() > 0){
                    flag = reConnectDevice();
                }
            }
        }
        return false;
platforms/android/app/src/main/java/io/cordova/hellocordova/CheckExitService.java
New file
@@ -0,0 +1,45 @@
package io.cordova.hellocordova;
import android.app.ActivityManager;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
import java.util.List;
public class CheckExitService extends Service {
    private String packageName = "io.cordova.hellocordova";
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        Toast.makeText(CheckExitService.this, "App要退出了", Toast.LENGTH_SHORT).show();
    }
    //service异常停止的回调
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        ActivityManager activtyManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activtyManager.getRunningAppProcesses();
        for (int i = 0; i < runningAppProcesses.size(); i++) {
            if (packageName.equals(runningAppProcesses.get(i).processName)) {
                Toast.makeText(this, "app还在运行中", Toast.LENGTH_LONG).show();
            }
        }
        return START_NOT_STICKY;
    }
    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(CheckExitService.this, "App检测服务开启了", Toast.LENGTH_SHORT).show();
    }
}
platforms/android/app/src/main/java/io/cordova/hellocordova/MainActivity.java
@@ -66,6 +66,10 @@
        webview = findViewById(appView.getView().getId());
        // 添加事件
        webview.addJavascriptInterface(new JsInterface(this, webview,MainActivity.this), JsInterface.JS_INTERFACE_NAME);
        Intent intent=new Intent(this,CheckExitService.class);
        getApplicationContext().startService(intent);
    }
    private void requsetPermission(){
platforms/android/app/src/main/java/io/hybird/jsbridge/JsInterface.java
@@ -35,7 +35,6 @@
        //server_ip = "192.168.7.130";
        service = new MyInteractionService(server_ip,this);
        getBattNames();
    }