我有一个
EventBus()
一直循环到我的另一个
Fragment
当我的应用程序启动时。当我将应用程序闲置至少30++分钟时,会得到以下堆栈跟踪:
10-11 10:23:46.315 3790-4797/com.jti.mikee.jti_pos W/libc: pthread_create failed: couldn't allocate 1040384-byte stack: Out of memory
10-11 10:23:46.315 3790-4797/com.jti.mikee.jti_pos E/dalvikvm: pthread_create (stack size 16384 bytes) failed: Try again
我也使用
ScheduleExecutorService()
在
onCreateView()
scheduledExecutorService = Executors.newScheduledThreadPool(4);
事件总线()
public static final Runnable mRunnable = new Runnable() {
@Override
public void run() {
EventBus.getDefault().post(new EB_TapCard());
}
};
onResume()
这是密码
@Override
public void onResume() {
Log.e("current_module",current_module);
super.onResume();
try {
EventBus.getDefault().register(this);
rfscanner = scheduledExecutorService.scheduleAtFixedRate(mRunnable, 0, 2, TimeUnit.SECONDS);
} catch (Throwable e) {
e.printStackTrace();
}
}
最后,这是我的
onPause()
@Override
public void onPause() {
try {
getContext().unregisterReceiver(broadcastReceiver);
} catch (Throwable e) {
e.printStackTrace();
}
rfscanner.cancel(true);
EventBus.getDefault().unregister(this);
super.onPause();
}
我在想,当我闲置我的设备时,runnable仍然在运行。如何暂停/恢复?非常感谢。
编辑
到目前为止,我已经尝试添加一个函数
Callback()
class CallBackThread extends Thread {
@Override
public void run() {
try {
RFCardInterface.waitForCardPresent(RFCardInterface.CONTACTLESS_CARD_MODE_AUTO, 1, -1);
if (RFCardInterface.isCallBackCalled &&
RFCardInterface.notifyEvent.eventID == RFCardInterface.CONTACTLESS_CARD_EVENT_FOUND_CARD) {
IDCatcher = StringUtility.ByteArrayToString(RFCardInterface.notifyEvent.eventData,
RFCardInterface.notifyEvent.eventData.length);
IDCatcher = IDCatcher.substring(9, 21).replace(" ", "");
Log.e("IDCatcher", IDCatcher);
EventBus.getDefault().post(new EBTest2());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}