Will Application.onCreate (Bundle) be called before BroadcastReceiver.onReceive (..)?

Just wanted to make sure Application.onCreate() would be guaranteed to be called before BroadcastReceiver.onReceive() ? Say you're waiting for a BOOT or SMS broadcast, can you be sure that Application.onCreate() has already been called once before you reach BroadcastReceiver.onReceive() ? Thanks

+9
android
source share
3 answers

public void onReceive(Context context, Intent intent) If you register a static receiver, the context is the application, otherwise it is the context in which you call registerReceiver with

+5
source share

The docs tell us the following:

public void onCreate ()

Called when the application starts, before any active, service, or recipient (except for content providers).

Found here: http://developer.android.com/reference/android/app/Application.html

+19
source share

An old question, however I can provide an updated answer and actual test results.

Initially, I assumed that I installed my onCreate () application in order to first save the application context, for example like this:

 @Override public void onCreate() { myapp.app_context = this; super.onCreate(); 

Now my recipient is declared like this:

  <receiver android:name=".myreceiver" android:permission="android.permission.RECEIVE_BOOT_COMPLETED"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.MY_PACKAGE_REPLACED" /> </intent-filter> </receiver> 

And upon receipt:

 @Override public void onReceive(Context context, Intent intent) { ctx = myapp.app_context; 

Got a stack trace showing that ctx (myapp.app_context) is NULL! Got 1 on the Android 6 Galaxy J7 device and 1 on the Android 9 Xperia XZ1.

0
source share

All Articles