首页 > 解决方案 > 如何在 Android 中使用计时器

问题描述

在我的应用程序中,我有一些工作的计时器
当我的应用程序在一段时间后运行时,我的应用程序冻结并且无法运行任何视图!
在这个计时器中,每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而不冻结我的应用程序?

标签: javaandroidandroid-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);
    }

}

推荐阅读