Abbath
Abbath

Reputation: 1012

my Alarm doen't start activity when phone lock is

I try to do alarm clock in my aplication, but when phone lock is there activity doest't start as in standart alarm clock. What I can do for decide this problem?

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.SECOND, 5);

    Intent intent = new Intent(this, AlarmReceiverActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,
        12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager am = 
        (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
            pendingIntent);

Upvotes: 0

Views: 886

Answers (1)

JanBo
JanBo

Reputation: 2943

This is "normal" behavior in such case.

To overcome it you need to acquire a wake_lock to the CPU.

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. If your alarm receiver called Context.startService(), it is possible that the phone will sleep before the requested service is launched. To prevent this, your BroadcastReceiver and Service will need to implement a separate wake lock policy to ensure that the phone continues running until the service becomes available.

this is from: http://developer.android.com/reference/android/app/AlarmManager.html

Similar problem: https://groups.google.com/forum/#!topic/android-developers/RAg9LJmH1oo


This is what you need: http://developer.android.com/reference/android/os/PowerManager.WakeLock.html

Problem acquiring wake lock from broadcast receiver

Upvotes: 2

Related Questions