简体   繁体   中英

Android - Is the Application class instantiated when a BroadcastReceiver is called?

is the Application class instantiated when a BroadcastReceiver is called? I have some logic in the custom Application class which is needed before the logic in onReceive of a BroadcastReceiver is run. The documentation says the Context argument in the onReceive method is the Context in which the receiver is running. I was not sure if it is the application in which the receiver is defined(statically in the manifest) or not.

Yes. The documentation for Application.onCreate notes:

Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created. If onCreate is called before your receiver is, it is by default instantiated first.

[1] http://developer.android.com/reference/android/app/Application.html#onCreate()

To answer this question you can try an expriment. Just extends the Application and log it's class in onReceive() :

Log.d(TAG, "Context is " + context.getClass().getName());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM