android 中每秒钟的 Http 请求

Http request for every seconds in android

我需要 post 每秒钟(实际上是 500 毫秒)一个 http 请求,另一方面,我必须每秒钟从 ble 接收数据,我已经尝试过倒计时计时器、服务、JobIntentService、 bt 所有这些都以 OutOfMemory Exception 结束。我不能使用 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)

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