0

I am writing app, which include service and timer. User set time of Timers. Timer must every day make work(even night). Timer (and service) is working when telephone active, but when telephone goes into sleep mode timer doesn't work(service still work). May be I must use the Handler with timer how here: Android timer? How-to? or I must use PowerManager and forcibly not allowed to sleep telephone?

I use timer within service

public class SampleService extends Service {

    ....

public int onStartCommand(Intent intent, int flags, int startId) {
    my_timer_start();
}

my_timer_start();{
    ltimer = new Timer();
    ltimer.scheduleAtFixedRate(new My_task(), time1, every_hour);
}


class My_task implements Runnable {

        @Override
        public void run() {
            // TODO Auto-generated method stub
...........................
        }
    }

void cancel_task() {
    ltimer.cancel();
    ltimer = null;
}

public void onDestroy() {
    super.onDestroy();
    cancel_task();
}



}

thanks for the help!

Community
  • 1
  • 1
Pavel K
  • 73
  • 9
  • Use `AlarmManager` to trigger a broadcast once an hour and use a `BroadcastReceiver` to start an `IntentService` to do the work for you. Your users will thank you for it as it won't flatten their batteries. – Squonk Aug 11 '15 at 21:42
  • Thanks for the help, your solution is right! – Pavel K Aug 23 '15 at 11:24

0 Answers0