I would still recommend you repeat your test with a Notification (possibly with a sound), as the system should take care of holding the wake lock(s) as needed for the duration of the sound.

As for implementing the wake locks in your code, to display an alert:

First, take a look at the docs, the basic use is "create a wake lock" - "acquire the wake lock" - "release the wake lock".

http://developer.android.com/reference/android/os/PowerManager.html

When creating your wake lock, these two PowerManager flags can be particularly useful: ACQUIRE_CAUSES_WAKEUP and ON_AFTER_RELEASE.

Second, the important thing is to acquire the wake lock early enough, inside onReceive is fine, so the device stays awake.

Third, it's very important to release the lock when no longer needed. Here is some pseudo code from my current project that starts a service based on wake alarms.

The alarm receiver is called while the system is holding its wake lock, and calls a static helper method in MyService, startForActionAlarm. This method creates the service's wake lock object if needed, and acquires it. Now it can return to AlarmReceiver.onReceive, which can return to Android, which can release its wake lock - but it's ok, because gWakeLock is now in effect.

A short time later, the system creates the service (if needed), and calls its onStartCommand. Here the service does something, and releases the wake lock that it originally acquired in startForActionAlarm.

AlarmReceiver:

    onReceive(Context context) {
        MyService.startForActionAlarm(context)
    }

MyService:

static WakeLock gWakeLock = null;

static WakeLock getWakeLock(Cotext context) {
       if (gWakeLock == null) {
            get PowerManager from context
            gWakeLock = power manager . newWakeLock(...)
        }
}

static void startForActionAlarm(Context context) {
    getWakeLock(this).acquire();

    Intent intent = new Intent(context, MyService.class);
    intent.setAction(ALARM_ACTION);

    if (context.startService(intent) == null) {
        // Failed to start itself, undo the lock
        getWakeLock(this).release();
    }
}

void handleStart(Intent intent, int startId) {
    if (intent.getAction().equals(ALARM_ACTION)) {
        getWakeLock(this).release();
    }
}

In your case, since the goal is to start an activity, the wake lock would perhaps be released at the end of in MyActivity.onStart, or perhaps a bit later, when the sound is done playing.

Hope this helps.

-- Kostya

24.10.2011 0:24, John Goche ?????:

%%%%% So now I must learn how to keep the CPU running or when the alarm goes off I will not be able to pop up the window and play the sound. When I leave the phone for a while the CPU hybernates or something it seems, so my RTC_WAKEUP calls the broadcast receiver but does
not pop up the intent it seems.

Can someone please teach me how to use wakelocks and exactly what they are so that I may fix this problem either by keeping the CPU running all the time, or in some more efficient way
if possible?

--
Kostya Vasilyev

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to