0

I am continuously receiving data with tcp socket. This is the code to reconnect if the socket is disconnected. In the service class, check if the socket state is disconnected. If the socket is disconnected, an alertdiolog pops up on the UI and counts down. When the countdown is over, reconnect.

However, when the screen is off and the screen is turned on again after 30 minutes, the UI is frozen. I don't know the cause for this. And is there any good way to monitor TCP Client socket status?

public class MntTCPsocket extends Service {
private static final int RECONNECT_TIMEOUT_3SEC = 3000;
private static final int RECONNECT_TIMEOUT_5SEC = 5000;
private static final int RECONNECT_TIMEOUT_10SEC = 10000;

private Handler handler = new Handler();
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();

private boolean socketStatus;
private boolean mStop = false;
private boolean mPause = false;
private int count = 1;

private Thread mMntTcpSocketThread;


private Context mContext;
private CountDownTimer countDownTimer = null;


public MntTCPsocket() {

}

@Override
public void onCreate() {
    super.onCreate();

    mContext = ((MainActivity) MainActivity.mContext);
    mMntTcpSocketThread = new MntNtripSocketThread();
    mMntTcpSocketThread.start();


}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {


    return START_STICKY;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}


public class MntNtripSocketThread extends Thread {

    private static final int RECONNECT_TIMEOUT_3SEC = 3000;
    private static final int RECONNECT_TIMEOUT_5SEC = 5000;
    private static final int RECONNECT_TIMEOUT_10SEC = 10000;

    private Handler handler = new Handler();
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    private boolean socketStatus;
    private boolean mStop = false;
    private boolean mPause = false;
    private int count = 3;

    boolean isTimerRunning;


    @Override
    public void run() {
        while (true) {
            lock.lock();
            showTCPReconnectAlertDialog(handler, 5000);
            condition.await();
           
            .... code .....

            lock.unlock();
        }

    }

    private void showTCPReconnectAlertDialog(Handler handler, int timerMillis) {
        PowerManager pm = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
        boolean isScreenOn = pm.isInteractive();


        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
               

                if (isScreenOn == true) { 
                   
                    AlertDialog dialog = new AlertDialog.Builder(mContext)
                            .setTitle("Tcp Reconnect Notify")
                            .setMessage("Try Tcp Reconnet ")
                            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    // TODO: Add positive button action code here
                                }
                            })
                            .setNegativeButton(android.R.string.no, null)
                            .create();
                  
                    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
                        @Override
                        public void onShow(final DialogInterface dialog) {
                            final Button defaultButton = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_NEGATIVE);
                            final CharSequence negativeButtonText = defaultButton.getText();

                            countDownTimer = new CountDownTimer(timerMillis, 1000) {
                                @Override
                                public void onTick(long millisUntilFinished) {
                                    if (((AlertDialog) dialog).isShowing()) {
                                        boolean isScreenOn = pm.isInteractive();
                                        if (isScreenOn == true) {
                                            defaultButton.setText(String.format(
                                                    Locale.getDefault(), "%s (%d)",
                                                    negativeButtonText,
                                                    TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) + 1
                                            ));
                                        } else {
                                            dialog.dismiss();
                                          
                                        }
                                    }
                                }

                                @Override
                                public void onFinish() {
                                    if (((AlertDialog) dialog).isShowing()) {
                                        dialog.dismiss();
                                    }
                                    // Reconnect code
                                    lock.lock(); 
                                    condition.signal(); 
                                    lock.unlock(); 
                                }
                            }.start();
                        }
                    });
                    dialog.show();
                } else {   
                  
                    countDownTimer = new CountDownTimer(timerMillis, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {

                        }

                        @Override
                        public void onFinish() {
                            // Reconnect code
                            lock.lock(); 
                            condition.signal(); 
                            lock.unlock(); 
                        }
                    }.start();
                }
            }
        }, 0);
    }
}

}

kyesia
  • 25
  • 3

0 Answers0