0

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.

user3289695
  • 740
  • 1
  • 9
  • 20
  • Try using `mAlarmManager.cancel(pi);` to cancel the alarm. – Aniket Kapse Feb 28 '14 at 05:53
  • I don't understand anything. If I use the common pattern and I acquire a wakelock and start an `IntentService` that is responsible of releasing the wakelock when finished, then, my device does not keep awake. But if I don't manage wakelocks and I not use an IntentService and do all the work in the `onReceive` method, the device keeps awake all the time. – user3289695 Feb 28 '14 at 06:02
  • The Alarm Manager holds a CPU wake lock as long as the alarm receiver's onReceive() method is executing. This guarantees that the phone will not sleep until you have finished handling the broadcast. Once onReceive() returns, the Alarm Manager releases this wake lock. This means that the phone will in some cases sleep as soon as your onReceive() method completes. – Aniket Kapse Feb 28 '14 at 06:20
  • This is what documentation says, but it is a lie. My device keeps awake all the time, after the onReceive method has finished. The only thing I have thought is that inside onReceive I have created an infinite loop accidentally, but I'm inspecting my code and I don't find it. – user3289695 Feb 28 '14 at 08:34

0 Answers0