如何在 Android 中使用计时器

How to use timer in Android

在我的应用程序中,我有一些作品的定时器
当我的应用程序 运行ning 一段时间后,我的应用程序 冻结 并且无法正常工作 View !
在这个定时器中每 500ms i 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 不冻结 我的应用程序?

应该类似于这段代码:

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

}

私人无效 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);

  }