I implemented a service in my Android application that starts a timer (using the standard mechanism java.util.Timer and java.util.TimerTask) to do some processing in the background at a predetermined interval.
public class BackgroundProcessingService extends Service {
private int interval;
private Timer timer = new Timer();
public void onCreate() {
super.onCreate();
startTimer();
}
@Override
public void onDestroy() {
timer.cancel();
super.onDestroy();
}
public int getInterval() {
return interval;
}
public void setInterval(int interval) {
this.interval = interval;
}
private void startTimer() {
timer.scheduleAtFixedRate( new TimerTask() {
public void run() {
}
}, 0, getInterval());
; }
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
The service starts / stops by my application as follows.
backgroundProcessingService = new Intent(getApplicationContext(), BackgroundProcessingService .class);
startService(backgroundProcessingService);
While the phone is active (there was no screen timeout), the service works fine, and the timer performs its task at the specified interval. Even when I exit the application (using the back button), the timer remains active.
, , . - (), , ( ) .
, , , , , .
, , , . PowerManager WAKE_LOCKS ( http://code.google.com/android/reference/android/os/PowerManager.html), ?