Correct way to use Tracking Lock (minimize battery usage) to handle incoming SMS messages

Let me start by admitting that I am learning (or afraid to learn) Android, and also doing what seems a little daunting.

While I think I have figured out the basic usage of BroadcastReceivers and I can use it to receive an SMS message and do some processing, I would like to have this processing done even when the device goes to sleep. After reading and asking the question ( this one ), I understand that I need to purchase a PARTIAL lock lock (to keep the processor active while the screen + keyboard is sleeping), handle incoming SMS requests, basically like the main messaging application (or such as Hangouts or other common messaging apps) that can receive incoming SMS messages and the notification LED blinks and vibrates (if configured to do so).

However, I am confused about the following:

  • The tow lock must be obtained when doing work and released when there is no work. If a wake-up lock is obtained and never released, you drain your battery much faster.
  • If the above value is true, then how is it expected that the SMS reader application will be immediately notified (perhaps using a BroadcastReceiver) of an incoming message, without having to constantly commit the snoop lock?
  • One of the ways that comes to my mind is to use a Service that mostly sleeps and only wakes up periodically to check if there are any SMS. However, this is contrary to the logic that incoming SMS and the corresponding broadcast of the Intent can happen at any time, clearly with sufficient opportunity for the application to skip the broadcast.

So how can you develop an application that:

  • Never misses, no incoming SMS and hopefully will not be notified as soon as it arrives (even if the device would otherwise sleep).
  • Consumes minimal battery, especially as the processing of SMS Broadcast Intent is very small and simple ... computationally minimal.

Edit (12 September 2014)

While I accepted the answer, I wanted to add what I understood, although I am still not 100% sure that my understanding is correct.

I realized that at a low level (operating system, firmware and HW device) on an Android device, when the device goes to sleep, there are several events that can cause "wake up". Network events, keyboard events, some (if not all) sensor events, timer events, etc. Make the device wake up and handle the event quickly, and try to get back to sleep as soon as possible. As far as I understand, the last bit is the key. This means that events such as an incoming call, incoming SMS, keystrokes, or other reasons leading to a broadcast always generate an intent and are delivered to all registered BroadcastReceivers. This part is provided. However, what is not enforced is that all BroadcastReceivers will have enough time toto spend as much time as needed to process the intent. Instead, they need to acquire a Wake lock (of the appropriate type) and ensure that the device does not go back to sleep until the Wake lock is released. Now, as far as I understand, there are many ways to acquire a Wake lock. This can be done directly in the BroadcastReceiver, but it seems not always intuitively clear what is the best and most efficient way to manage it, st it is held for the least amount of time and released as soon as it is not needed. To do this, the SDK provides a "WakefulBroadcastReceiver" mechanism, which makes it a task to write software that simplifies BroadcastIntent when a tracking lock is required.Then there is the alternative WakefulIntentService developed by CommonsWare (if I am not mistaken, this is not from the SDK, but from a third party).

I hope I'm right :)

Edit (September 13, 2014)

More information is available at this SO Q&A

+3


source to share


1 answer


There is a broadcast for incoming sms (a quick google search gave android.provider.Telephony.SMS_RECEIVED

). So you've set up a receiver for this ( Google swift ). In the receiver, you run the WakefulIntentService (this manages your wakelocks). It's all.



WIS is the answer to your other question.

+2


source







All Articles