| | |
| | | public boolean isConncet = false; |
| | | private final Timer timer = new Timer(); |
| | | |
| | | |
| | | @Override |
| | | public void onCreate() { |
| | | super.onCreate(); |
| | |
| | | isAidl = true; |
| | | AidlUtil.getInstance().connectPrinterService(this); |
| | | initEasySocket(false, null); |
| | | socketConnect(); |
| | | |
| | | CrashReport.initCrashReport(getApplicationContext(), "45551598b8", true); |
| | | } |
| | | |
| | |
| | | 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不生效问题 |
| | |
| | | .setSocketAddress(new SocketAddress(ip, port)) |
| | | // 定义消息协议,方便解决 socket黏包、分包的问题 |
| | | .setReaderProtocol(new ScoketMessageProtocol()) |
| | | .setMaxWriteBytes(1024) |
| | | .setMaxReadBytes(1024) |
| | | .setRequestTimeout(10 * 1000) |
| | | .setMaxWriteBytes(150) |
| | | .setMaxReadBytes(150) |
| | | .setRequestTimeout(5 * 1000) |
| | | .build(); |
| | | |
| | | // 初始化 |
| | |
| | | // 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(); |
| | | } |
| | | } |
| | | }; |
| | | |
| | | |
| | | } |