如何在Android中使用计时器

时间:2018-12-18 06:27:48

标签: java android android-thread

在我的应用程序中,我有计时器用于某些工作。
当我的应用程序运行一段时间后,我的应用程序冻结,并且无法在任何视图中运行!
在此计时器中,每 500毫秒emit socket.io

我的代码:

   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);
        }
    });

我如何在另一个thread中运行此计时器,并且不冻结我的应用程序?

2 个答案:

答案 0 :(得分:1)

应该类似于以下代码:

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);
    }

}

答案 1 :(得分: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);

  }