In my application, I've got an alarm which triggers a service which downloads information from the internet and shows a notification.
Here's a simplified version of my code:
MyActivity contains this:
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 20);
Intent intent = new Intent(this, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 20000, pendingIntent);
And AlarmService looks like this:
public class AlarmService extends Service {
@Override
public void onCreate() {
new myAsyncTask().execute();
}
private class myAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... args) {
//Download stuff
return null;
}
@Override
protected void onPostExecute(Void arg) {
//Show notification
}
}
}
I don't really understand when to use wake-locks, so my question: in this case, should I use a wake-lock and if so, where should I start and stop it?
Thanks in advance