Do I need a tracking lock in my translator if I do not start a service or activity?

I have a broadcast receiver triggered by an alarm (scheduled with AlarmManager). In this receiver, I only request a register from the database and trigger a notification. I read that tracking lock is needed when a service or activity is started from the broadcast receiver, but do I need a wake lock if I want to show only a notification (in the notification panel)?

+6
source share
2 answers

In this receiver, I only request a register from the database and trigger a notification.

Do not do database I / O in the main thread of the application.

I read that tracking lock is needed when a service or action is started from the broadcast receiver, but do I need a tracking lock if I want to show only a notification (in the notification panel)?

In general, no, you won’t need the WakeLock from BroadcastReceiver , not even the one that is triggered by the _WAKEUP . AlarmManager guarantees in this case that it will support waking up the device using its own WakeLock .

However, again, in this case, you really do not need to do database I / O in the main thread of the application, and onReceive() is called in the main thread of the application. The correct template here is that you move the "register request from the database and start the notification" to the IntentService launched by your BroadcastReceiver so that the work is done in the background thread. This will require WakeLock , because now you are doing work outside of onReceive() . I have a WakefulIntentService that manages WakeLock for you if you want to use it.

+16
source

Yes, it is necessary. I remember that at the kernel level, the processor will run for about 5 seconds. Therefore, if you cannot finish sending your notification within 5 seconds, you need to grab the tracking lock. And let him go after you finish your work.

-1
source

All Articles