Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BroadcastReceiver receives events with delay after reboot

I try to receive PHONE_STATE intents with next BroadcastReceiver

<receiver android:name=".CallReceiver"
    android:enabled="true"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE" />
        <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

But after rebooting my Nexus 5X and making the call to this device, ringing events can be received after the call has been already finished a minute ago. How can I fix it? Any ideas?

like image 692
shmakova Avatar asked Jan 12 '17 15:01

shmakova


People also ask

What is the life cycle of BroadcastReceiver?

A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent) . Once your code returns from this function, the system considers the object to be finished and no longer active.

What is the role of the onReceive () method in the BroadcastReceiver?

Creating a BroadcastReceiver The onReceiver() method is first called on the registered Broadcast Receivers when any event occurs. The intent object is passed with all the additional data. A Context object is also available and is used to start an activity or service using context. startActivity(myIntent); or context.

What is the method name in BroadcastReceiver receive the message?

onReceive. This method is called when the BroadcastReceiver is receiving an Intent broadcast.

Does broadcast receiver work in background?

A broadcast receiver will always get notified of a broadcast, regardless of the status of your application. It doesn't matter if your application is currently running, in the background or not running at all.


2 Answers

According to CommonsWare in this answer-

There are many, many apps that want to get control at boot time. How quickly yours will get its turn will depend on many variables, such as the number of installed apps, the CPU speed of the device, the amount of system RAM on the device, etc.

Also, starting an activity from a BroadcastReceiver at boot time is fairly evil. If you want to be the first thing the user sees after a reboot, write a home screen implementation.

All you can do is give priority like this -

 <action android:name="android.intent.action.BOOT_COMPLETED" android:priority="999"/>
like image 59
karanatwal.github.io Avatar answered Nov 14 '22 23:11

karanatwal.github.io


this because there any other Receiver listen to this broadcast, you can try to improve you Recevier' priority just like that

<intent-filter android:priority="2147483647">  
<action android:name="android.intent.action.BOOT_COMPLETED" />  
</intent-filter>
like image 20
HelloCsl Avatar answered Nov 15 '22 00:11

HelloCsl