Facebook's "Messenger" has a SMS Broadcast Receiver that takes highest priority after reboot

JavaAndroidFacebookBroadcastreceiver

Java Problem Overview


Facebook's Messenger, has a priority of 2147483647, for their android.provider.Telephony.SMS_RECEIVED broadcast receiver, declared in their manifest.

(It's sad we are forced to not follow the documentation's standards of max priority being 1000 because other apps make their own rules)

I understand that if my priority is also set to the ridiculously high level of 2147483647 that I'd have to have my app installed first to take precedence over any "ties". No problem, I made a screen to alert users what apps would probably need to be uninstalled then re-installed after my app to function properly.

But, here's the problem - Everything works fine, but as soon as the phone reboots, "Messenger" starts getting precedence over my app. I've looked all over the place to see how they could do this black magic. How do they do this? How do I get priority after reboot, when my app is installed first?

The only thing that has come to mind so far, is package names being ordered alphabetically on boot when registering broadcast receivers.

com. f acebook > com. s trikeforcezero

I was about to attempt to register my broadcast receiver on android.intent.action.BOOT_COMPLETED but I have a feeling this won't work.

Messenger also has another "low priory broadcast receiver" for android.provider.Telephony.SMS_RECEIVED set to -1

Java Solutions


Solution 1 - Java

For sure:

If the user downloads your app before facebook, then your receiver will take precedence although both have the priority 2147483647. After a reboot, Facebook's receiver takes precedence although both have the same priority and yours was installed before. Therefore, for sure, the trick is in their BootReceiver.

Guesses:

  1. As a first step, add a boot receiver with the same priority 2147483647 (although priority has no effect but just add it) to your app. Try to install your app before facebook's app and restart the phone. It might be related to the fact that your process starts before facebook in this case and your sms receiver will start before facebook's.

  2. Do the same thing but rename your package name to a.a.a just to have precedence over facebook Alphabetically.

  3. In your Boot Receiver, try to add the following code:

    ComponentName component;
    component=new ComponentName(this, SmsReceiver.class);
    getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
    component = new ComponentName(this, SmsReceiver.class);
    getPackageManager().setComponentEnabledSetting(component, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    
  4. It might be useful to try to know whether your boot receiver is taking precedence over facebook's boot receiver. I am not sure if that is possible.

  5. Finally, there is the process which can be differentiated in the manifest but am not sure if that can be handy in anyway.

Solution 2 - Java

Even though max priority value is 1000, Facebook used largest integer value as priority to be first. So, I would say you should also keep the same (2147483647) if you want to have a chance. Alert users to uninstall and install Facebook after your app is installed.

To get precedence after rebooting: You can't set priority for boot_completed so, try to Explore android source-code especially SystemServer.java and see once SystemServer is up and running & once booting is completed, what order is used to start apps that are registered for boot_completed.

Solution 3 - Java

My suspicion would be that ties are not broken by the app installed first but the app which is run first. In the case where you are manually installing and uninstalling applications you are manually controller the execution order. Then, when you reboot, the execution order is relinquished to Android's launch behavior. It would be easy to test if this occurs alphabetically: build a quick test app name aaa and see if it maintains priority after reboot. If the boot order does not change alphabetically, comment and I'll go work out how launch order is setup.

Solution 4 - Java

This reveals some bug in Android mechanism since the number you think they are putting in the manifest isn't really the number. 2147483647 is really (-1) which is 2^31. Why do you think that it is matter which application installed first?

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStrikeForceZeroView Question on Stackoverflow
Solution 1 - JavaSherif elKhatibView Answer on Stackoverflow
Solution 2 - JavaSenthil PrabhuView Answer on Stackoverflow
Solution 3 - JavaJoshcodesView Answer on Stackoverflow
Solution 4 - JavaBushView Answer on Stackoverflow