我需要每秒鐘(實際上是 500 毫秒)發布一個 http 請求,另一方面,我必須每秒鐘從 ble 接收資料,我已經嘗試過倒數計時器、服務、JobIntentService,但它們都以OutOfMemory 例外告終. 我不能使用 workManager,因為 WM 的最小間隔是 15 分鐘。任何建議或指南?
java.lang.OutOfMemoryError: Could not allocate JNI Env at java.lang.Thread.nativeCreate(Native Method) at java.lang.Thread.start(Thread.java:730) at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:941) at java.util.concurrent.ThreadPoolExecutor.processWorkerExit(ThreadPoolExecutor.java:1009) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1151) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) at java.lang.Thread.run(Thread.java:761)
uj5u.com熱心網友回復:
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
// call request method
}
}, REQUEST_PERIOD_AS_MILISECONDS);
// You need to cancel it when you are done with it as following:
if (timer != null) {
timer.cancel();
timer = null;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/376637.html
