Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android O : PHONE_STATE broadcast limitation

I have been trying to do something similar to truecaller app, where my app is supposed to show a screen after a call gets hung up. Was achieving this by registering android.intent.action.PHONE_STATE implicit broadcast in the manifest file.

But it is not working if I change the app to target Android O, because of the Android O broadcast limitation, and I'm trying to figure out an alternative solution to this use case.

Alternative solutions suggested in android docs: Job scheduler or register a service with context.

Job scheduler: Because of the Job scheduler optimizations there will be some delay to receive the callback. So it will affect the user experience if our app screen is shown a few min after the phone call and polling to check for new call logs every few seconds causes battery drain issue.

Register service with context in Java: I want the behavior to work even if the app is not active or alive. This will not work if the system kills the Service.

Register a Foreground Service: This requires a notification to be shown to the user all the time, which would be spam the user, and running a service 24/7 consumes lots of resources which defeats the whole purpose of broadcast limitation.

Please suggest an alternate solution so that the user experience remains the same.

Thanks in advance

like image 922
Praveen Kumar C Avatar asked Aug 16 '17 14:08

Praveen Kumar C


People also ask

What is the time limit of broadcast receiver in Android?

As a general rule, broadcast receivers are allowed to run for up to 10 seconds before they system will consider them non-responsive and ANR the app.

How many types of broadcast are there in Android?

There are two types of broadcast receivers: Static receivers, which you register in the Android manifest file. Dynamic receivers, which you register using a context.

When would you use a broadcast receiver Android?

1.1. Definition. A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.

What is protected broadcast in Android?

To prevent arbitrary processes from sending sensitive broadcast Intents, Android allows the declaration of sensitive broadcast actions as “protected” by using the 'protected-broadcast' element in an authorized app's AndroidManifest.


2 Answers

Eventually, the action was added to the "Implicit Broadcast Exceptions" list so you can add ACTION_PHONE_STATE_CHANGED to your manifest and it will work:

https://developer.android.com/guide/components/broadcast-exceptions

ACTION_CARRIER_CONFIG_CHANGED, TelephonyIntents.ACTION_*_SUBSCRIPTION_CHANGED, "TelephonyIntents.SECRET_CODE_ACTION", ACTION_PHONE_STATE_CHANGED, ACTION_PHONE_ACCOUNT_REGISTERED, ACTION_PHONE_ACCOUNT_UNREGISTERED

OEM telephony apps may need to receive these broadcasts.

like image 160
Gaket Avatar answered Oct 04 '22 19:10

Gaket


You have only one solution, use a foreground service and register the broadcast receiver in the service.

like image 37
greywolf82 Avatar answered Oct 04 '22 18:10

greywolf82