Home > Back-end >  Http request for every seconds in android
Http request for every seconds in android

Time:12-08

I need to post a http request for every seconds(actually 500 milliSeconds) and on other hand i have to receive data from ble for every seconds and i've tried Countdown timer, Service, JobIntentService, bt all of them end up with OutOfMemory Exception. I Cant use workManager bcoz the min Interval of WM is 15mins. Any suggestion or a guide ?

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)

CodePudding user response:

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