TCP自动重连

每2S发一次心跳包:

    public Thread _thread;
    public boolean ThreadRunning = true;   //活动销毁置为false
    private boolean isLogined = false;

    //启动线程
    _thread=new Thread(TpcConnectCheckingRunable);
    _thread.start();

    //线程
    int Tickcount=60;
    Runnable TpcConnectCheckingRunable = new Runnable() {
        public void run() {
            while (ThreadRunning) {

                try {
                    if (TCPHandle._asysocket == null) {
                        System.out.println("TCPHandle Connect");
                        if (isOpenNetwork()) {
                            TCPHandle.Connect();
                        }
                        else{
                            System.out.println("网络断开");
                        }
                    }
                    if (TCPHandle._asysocket != null && isLogined == true) {
                        if (TCPHandle.Uid.isEmpty() || TCPHandle.TOken.isEmpty()) {
                            //如果TCP没断,且没有签退则自动登录
                            RxBus.get().post(BusAction.OnNet_Login, "");
                        }
                        else{
                            if (Tickcount<1)
                            {
                                try{
                                    TCPHandle.writedData(Protrol.getInstance().TicketData());
                                    Tickcount=60;
                                }
                                catch (Exception ex)
                                {
                                    System.out.println(ex);
                                }
                            }
                            Tickcount--;

                        }
                    }
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };

判断某一个类是否存在任务栈里面

/**
     * 判断某一个类是否存在任务栈里面
     * @return
     */
    public static boolean isExsitMianActivity(Context context,String classname){
        Intent intent = new Intent();
        intent.setClassName(context.getPackageName(), classname);

//        方法一:
        if (context.getPackageManager().resolveActivity(intent, 0) != null) {
            System.out.println("我是第一个判断,执行啦");
            return true;
            // 说明系统中存在这个activity
        }

//        方法二:
        if(intent.resolveActivity(context.getPackageManager()) != null) {
            System.out.println("我是第二个判断,执行啦");
            return true;
            // 说明系统中存在这个activity
        }
//        方法三:
        List list = context.getPackageManager().queryIntentActivities(intent, 0);
        if (list.size() > 0) {
            System.out.println("我是第三个判断,执行啦");
            return true;
            // 说明系统中存在这个activity
        }
        return false;
    }

判断某个界面是否在前台:

/**
     * 判断某个界面是否在前台
     * @param context   Context
     * @param className 界面的类名
     * @return 是否在前台显示
     */
    public static boolean isForeground(Context context, String className) {
        if (context == null || TextUtils.isEmpty(className))
            return false;
        ActivityManager am = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
        List list = am.getRunningTasks(1);
//        boolean flag=false;
        for (ActivityManager.RunningTaskInfo taskInfo : list) {
            if (taskInfo.topActivity.getShortClassName().contains(className)) { // 说明它已经启动了
//                flag = true;
                return true;
            }
        }
        return false;
    }

你可能感兴趣的:(Android开发)