I have an application which has to execute every days at 00:00, so I have used AlarmManager
to set alarms.
This is the class I use to set an alarm:
package bembibre.coolstar.windowsmobilewidget.backend.alarms;
import java.util.Calendar;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
public class AlarmSetter{
private Context mContext;
private AlarmManager mAlarmManager;
public AlarmSetter(Context context){
mContext = context;
mAlarmManager =
(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
}
public void setAlarm(Calendar when){
Intent i = new Intent(mContext, OnAlarmReceiver.class);
PendingIntent pi =
PendingIntent.getBroadcast(mContext, 0, i, PendingIntent.FLAG_ONE_SHOT);
mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
}
}
When clock reaches 00:00, the onReceive()
method of my class onAlarmReceiver
is called, the device always wakes up automatically (I suppose AlarmManager is who does this) but when the onReceive
method finishes, the device keeps awake continuously and never goes to sleep. I'm not managing wakelocks because AlarmManager
keeps device awake until onReceive
method finishes, and the work I have to do there is very light. My onAlarmReceiver
class is this:
package bembibre.coolstar.windowsmobilewidget.backend.alarms;
import bembibre.coolstar.windowsmobilewidget.ExtendedAppWidgetProvider;
import bembibre.coolstar.windowsmobilewidget.ListItemClickService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class OnAlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ExtendedAppWidgetProvider.setDayChangeAlarm(context);
new ListItemClick(context).update();
}
}
The method update()
is very light. I've read that what I should do is to make ListItemClick
an IntentService, acquire a wakelock before calling update()
, start the IntentService
, and inside of it, when it finishes, release the wakelock. But this wakelock would be mine. The AlarmManager not released wakelock would continue keeping device awake until the infinite. So I don't understand nothing. It is supposed that if there are two wakelocks, the AlarmManager wakelock and the mine, and I release the mine, the AlarmManager wakelock would continue unreleased.