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