左晓为主开发手持机充值管理机
zuoxiao
2023-11-29 9c11fb9a45b0f1ff2a86eb139078e5361216434b
app/src/main/java/com/dayu/recharge/MyApplication.java
@@ -45,6 +45,7 @@
    public boolean isConncet = false;
    private final Timer timer = new Timer();
    @Override
    public void onCreate() {
        super.onCreate();
@@ -52,7 +53,7 @@
        isAidl = true;
        AidlUtil.getInstance().connectPrinterService(this);
        initEasySocket(false, null);
        socketConnect();
        CrashReport.initCrashReport(getApplicationContext(), "45551598b8", true);
    }
@@ -66,13 +67,20 @@
        isAidl = aidl;
    }
    public IpBean ipBean;
    /**
     * 初始化EasySocket
     */
    public void initEasySocket(boolean isCreate, SocketNet.CreateBack createBack) {
        try {
            this.createBack = createBack;
            IpBean ipBean = BaseDaoSingleton.getInstance(MyApplication.myApplication).ipDao().findFirst();
            ipBean = BaseDaoSingleton.getInstance(MyApplication.myApplication).ipDao().findFirst();
            if (ipBean == null) {
                ipBean = new IpBean();
                ipBean.setIp("dayuyanjiuyuan.top");
                ipBean.setPort(8888);
            }
            if (ipBean != null) {
                try {
                    //处理当连接上IP后修改ip不生效问题
@@ -225,9 +233,9 @@
                .setSocketAddress(new SocketAddress(ip, port))
                // 定义消息协议,方便解决 socket黏包、分包的问题
                .setReaderProtocol(new ScoketMessageProtocol())
                .setMaxWriteBytes(1024)
                .setMaxReadBytes(1024)
                .setRequestTimeout(10 * 1000)
                .setMaxWriteBytes(150)
                .setMaxReadBytes(150)
                .setRequestTimeout(5 * 1000)
                .build();
        // 初始化
@@ -235,43 +243,6 @@
//        EasySocket.getInstance().subscribeSocketAction(socketActionListener);
        EasySocket.getInstance().subscribeSocketAction(SocketNet.getInstance().socketNet.socketActionListener);
    }
    /**
     * 定时重连服务器
     */
    private void socketConnect() {
        try {
            TimerTask task = new TimerTask() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    Message message = new Message();
                    message.what = 1;
                    handler.sendMessage(message);
                }
            };
//            timer.schedule(task, 5000, 1000 * 60 * 3);
            timer.schedule(task, 5000, 1000 * 60 * 3);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            // 要做的事情
            super.handleMessage(msg);
            try {
                SocketNet.getInstance().isConnect(MyApplication.this);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
}