views:

45

answers:

0

I'm using notifications in my simple timer app (early testing stage), and found that maybe 5% to 20% of the times I fired a notification, the sound did not play. The notification appeared correctly in all other respects, including LED flash, but was silent. I have confirmed with temporary extra log lines that the sound URIs I'm using are sensible, and are in fact identical in cases that work and cases that don't. I'll summarise the code setting up the notification, in case I'm doing something monumentally stupid:

// in a place called from onReceive()...
NotificationManager notifications = ...;
Notification n = new Notification(R.drawable.icon, text, nextMillis);
n.setLatestEventInfo(...);
n.flags = Notification.FLAG_NO_CLEAR | FLAG_SHOW_LIGHTS;
n.ledOnMS = 250; n.ledOffMS = 1250; n.ledARGB = 0xff2222ff;
n.audioStreamType = AudioManager.STREAM_ALARM;
n.sound = myUri;
notifications.notify((int)id, n);

Here's the real thing. The only common factor I've found is that in the problematic cases, the phone was asleep (screen off for more than 30 seconds). The notification is requested from a BroadcastReceiver, having been woken up from an RTC_WAKEUP alarm. The failure mode that comes to mind is that the device goes back to sleep too early, but nothing in logcat suggests this. In fact I can't see any difference in logcat between success and a missing sound. In an attempt to work around sleep issues, if there are any, I have added (in this commit) this WakeLock temporarily:

+ PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Timer");
+ wl.acquire();
+ Log.d(TimerActivity.TAG, "Got wake lock");
  notifications.notify((int)id, n);
  Log.d(TimerActivity.TAG, "Notified!");
+ try {
+     Thread.sleep(1000);
+ } catch (InterruptedException e) { e.printStackTrace(); }
+ Log.d(TimerActivity.TAG, "Released wake lock");
+ wl.release();

...and the problem has not been seen since I added this, but that might be coincidence, and this is obviously an ugly hack that I'd rather avoid when I actually release this project (currently only one other person uses it that I know of).

This has been seen on two Nexus Ones running Android 2.2 (Froyo). It was not seen on 2.1, but wasn't used much on that version, and was not seen on 1.6 on a G1, where it was used for a few months. It has been seen at all times of day, with sounds from the SD card and from internal storage. I've also searched the Android bug tracker a bit, to no avail.