开发者

how do i ensure that a service is running even if the phone is on standby?

开发者 https://www.devze.com 2023-01-17 17:35 出处:网络
I have a service that spawns a thread which runs a task every 3 seconds. class MyService extends Service {

I have a service that spawns a thread which runs a task every 3 seconds.

class MyService extends Service {
  private Timer timer = new Timer();

  public int onStartCommand(Intent intent, int flags, int startId) {
    TimerTask task = new TimerTask() {
      public void run() {
        // something important
      }
    };

    timer.scheduleAtFixedRat开发者_开发技巧e(task, 0, 3000);
    return Service.START_STICKY;
  }
  ...
}

My service runs fine when my phone is on, but when my phone is on standby, the service is often not responding.

Any clues as to how I can ensure my service is running while on standby?


Maybe use alarms via AlarmManager class.

0

精彩评论

暂无评论...
验证码 换一张
取 消