Reputation: 1213
I'm extending the NotificationListenerService in order to catch notifications being posted to the device. This has been working perfectly fine without doing anything special (like using wakelocks). I used to use a background thread to do some work (doing HTTP posts).
Lately I've started doing more work in the Service (about 1 sec CPU work on a Nexus 4 at wake) and I've dropped the background thread.
I've noticed that sometimes the device does not wake for doing the work, doing it minutes later or immediately when I turn the screen back on. I've added a partial wake-lock hoping it would solve the problem. Sadly it still shows random behavior.
Any idea how I can debug this any further and can anyone tell me whether my approach is correct?
PowerManager.WakeLock wakeLock;
static String wakeLockTag = "NotificationListenerWakeLock";
@Override
public void onNotificationPosted(StatusBarNotification sbn) {
acquireWakeLock();
// Do work
releaseWakeLock();
}
private void acquireWakeLock() {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (wakeLock == null) {
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakeLockTag);
}
if (!wakeLock.isHeld()) {
wakeLock.acquire();
}
}
private void releaseWakeLock() {
if (wakeLock != null && wakeLock.isHeld())
wakeLock.release();
}
Edit:
Looks like the wakelock gets released prematurely. It ends up in these two messages being spammed in LogCat for about 2 minutes:
02-11 20:12:19.557: D/dalvikvm(16564): WAIT_FOR_CONCURRENT_GC blocked 21ms
02-11 20:12:19.617: D/dalvikvm(16564): GC_CONCURRENT freed 1032K, 10% free 19831K/21820K, paused 1ms+1ms, total 20ms
02-11 20:12:19.617: D/dalvikvm(16564): WAIT_FOR_CONCURRENT_GC blocked 9ms
02-11 20:12:19.677: D/dalvikvm(16564): GC_CONCURRENT freed 1075K, 10% free 19794K/21820K, paused 1ms+1ms, total 17ms
What am I doing wrong?
Upvotes: 0
Views: 1021
Reputation: 34086
Don't know about the garbage collector (I guess all the runs happen at once)
You could try to delegate to a WakefulIntentService. It holds a wake lock that covers the gap between onReceive()
and doWakefulWork()
which you must override. It will delegate to a thread and release the lock on completion. If onNotificationPosted
wakes the CPU then you may need to use a WIS there.
But it seems more probable that simply the phone is not woken up
See:
Upvotes: 0