0

In my application i have timer for some works.
When my application running after some time my application freeze and not work any View !
In this timer every 500ms i emit socket.io

My Codes:

   AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            socketPingTimer.scheduleAtFixedRate(new TimerTask() {
                @Override
                public void run() {
                    if (isSendSocketPing) {
                        checkSocketPingTimer += startSocketPingTimer;
                        if (checkSocketPingTimer == sendSocketPingTimer) {
                            currentTimerForSocket = System.currentTimeMillis();
                            try {
                                detailSocketUtils.getSendRTTforPing(currentTimerForSocket + "");
                            } catch (Exception e) {
                            }
                        }
                        //Show ping (from search)
                        Constants.currentActivity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (isShownPing) {
                                    detailToolbar_ping.setVisibility(View.VISIBLE);
                                    if (checkSocketPingTimer > 500) {
                                        detailToolbar_ping.setText(checkSocketPingTimer + "");
                                        detailToolbar_ping.setTextColor(Color.RED);

                                    } else {
                                        detailToolbar_ping.setText(checkSocketPingTimer + "");
                                        detailToolbar_ping.setTextColor(Color.GREEN);
                                    }
                                } else {
                                    detailToolbar_ping.setVisibility(View.GONE);
                                }
                            }
                        });
                        socketPing = checkSocketPingTimer;
                    }
                }
            }, 500, startSocketPingTimer);
        }
    });

How can i run this timers in another thread and not freeze my app ?

Dr. Jake
  • 249
  • 2
  • 3
  • 9
  • 1
    Do not use `AsyncTask` to execute a timer. Instead, use a `Handler`. Here is an example : https://stackoverflow.com/a/4598737/783707 – Ankur Aggarwal Dec 18 '18 at 07:20
  • You can use CountDownTimer which have a tick abstract function for any execution that you want to achieve and tick can be changeable. – Rudrik Patel Dec 18 '18 at 07:30
  • Agreed - `Handler` is more suitable (rather than creating a new `Runnable` to update UI every 500 ms). Here is even a shorter version that will help you getting your `runOnUiThread` logic out of there https://stackoverflow.com/a/15685768/4062197 – ymz Dec 18 '18 at 07:36
  • @AnkurAggarwal , thanks my friend. can you send to me code with my above codes? please dear friend – Dr. Jake Dec 18 '18 at 08:03
  • @ymz , thanks my friend. can you send to me code with my above codes? please dear friend – Dr. Jake Dec 18 '18 at 08:03

2 Answers2

1

It should be something similar to this code:

class MyActivity extends Activity
{
    private void executeLoop()
    {
        Handler myHandler = new Handler() 
        {
            public void handleMessage(Message msg) 
            {
                if (isShownPing) 
                {
                    detailToolbar_ping.setVisibility(View.VISIBLE);

                    if (checkSocketPingTimer > 500) {
                        detailToolbar_ping.setText(checkSocketPingTimer + "");
                        detailToolbar_ping.setTextColor(Color.RED);

                    } else {
                        detailToolbar_ping.setText(checkSocketPingTimer + "");
                        detailToolbar_ping.setTextColor(Color.GREEN);
                    }
                } else 
                {
                    detailToolbar_ping.setVisibility(View.GONE);
                }

            }
        }

        socketPingTimer.scheduleAtFixedRate(new TimerTask() 
        {
            @Override
            public void run() 
            {
                if (isSendSocketPing) 
                {
                    checkSocketPingTimer += startSocketPingTimer;
                    if (checkSocketPingTimer == sendSocketPingTimer) {
                        currentTimerForSocket = System.currentTimeMillis();
                        try {
                            detailSocketUtils.getSendRTTforPing(currentTimerForSocket + "");
                        } catch (Exception e) {
                        }
                    }

                    myHandler.sendEmptyMessage();

                    socketPing = checkSocketPingTimer;
                }
            }
        }, 500, startSocketPingTimer);
    }

}
ymz
  • 6,602
  • 1
  • 20
  • 39
1

private void startTimerAtFixRate() {

    android.os.Handler handler = new android.os.Handler();

    Runnable updateTimerThread = new Runnable() {
        public void run() {
            //write here whatever you want to repeat
            // Like I called Log statement
            // After every 1 second this below statement will be executed
            Log.e("CALLED-->", "TRUE");

            handler.postDelayed(this, 1000);
        }
    };

    handler.postDelayed(updateTimerThread, 100);

  }
Rakesh Verma
  • 766
  • 6
  • 14
  • I love the re-use of handler with `handler.postDelayed(this, 1000)` instead of just using `scheduleAtFixedRate` as a looper.. you got my upvote :) – ymz Dec 19 '18 at 09:35