Well, here is what I am doing now: I acquire a wakelock inside the broadcast receiver and release it from my activity. The only problem I have is that the play() method is asynchronous. I would like to be notified when the sound stops playing via a callback but I see no method for doing so here:
http://developer.android.com/reference/android/media/Ringtone.html#play%28%29 I wonder whether there is a way to detect this without having to poll calling isPlaying() every second or so. Thanks for your replies, John Goche class WakeLockManager { static void getWakeLock(Context context) { if (count++ == 0) { PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.ON_AFTER_RELEASE, TAG); wl.acquire(); } } static void putWakeLock() { if (count > 0 && --count == 0) wl.release(); } private static final String TAG = "WakeLockManager"; private static int count = 0; private static WakeLock wl; } On Mon, Oct 24, 2011 at 1:31 AM, John Goche <johngoch...@googlemail.com>wrote: > > Hi, > > I have done some more testing. It is working much better now in that each > time there is an > alarm a window pops up. In one last case though only after sliding the > phone guard up I did > find that the alarm window started to play late, so it is as though indeed > the phone went back > to sleep right after firing the activity (before such activity finished > playing the sound). I must > also admit that I now acquire the lock at the very beginning of the > broadcast receiver that > fires my activity, this may or may not have made the difference as well > besides changing > from a partial wakelock to a full wakelock. Anyways, I may have to hold the > wakelock > even longer to see if this other problem concerning the window only popping > up when > I unlock the phone goes away. (cause I am seeing the phone light up and > quickly light > down but then no sound). Actually as I speak even the intent does not make > it in some > cases, I just see the lightup, so I am going to have to recode. > > Thanks, > > John Goche > > > On Mon, Oct 24, 2011 at 12:25 AM, John Goche > <johngoch...@googlemail.com>wrote: > >> >> Thanks Kostya, >> >> I changed my wakelock from partial wakelock to full wakelock and that >> seems to have fixed things, albeit >> somewhat strangely. I now see the screen light up on each timer >> expiration. I am going to do some more >> testing and if things go wrong I will try releasing the wakelock from >> inside the activity and implementing a >> singleton pattenrn instance to hold the wakelock like you did. Right now >> it seems strange to me that if a >> wakelock is acquired (especially a full wakelock) that the CPU would go >> back to sleep as soon as I >> release it but maybe this is indeed the case in some scenarios, so I am >> going to investigate this >> matter somewhat further (although I hate having to waste so many minutes >> just waiting for the >> CPU to power down and then see if the alarm comes back on but anyways...). >> >> Thanks a lot, your post was very helpful! >> >> Let me know if you have any other ideas or wisdom to share, >> >> John Goche >> >> >> On Sun, Oct 23, 2011 at 10:47 PM, Kostya Vasilyev <kmans...@gmail.com>wrote: >> >>> 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 >>> >> >> > -- 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