[英]Android - Is the Application class instantiated when a BroadcastReceiver is called?
is the Application
class instantiated when a BroadcastReceiver
is called? 调用BroadcastReceiver
会实例化Application
类吗? I have some logic in the custom Application class which is needed before the logic in onReceive
of a BroadcastReceiver
is run. 在自定义Application类中,我有一些逻辑,在运行BroadcastReceiver
onReceive
中的逻辑之前,需onReceive
。 The documentation says the Context
argument in the onReceive
method is the Context
in which the receiver is running. 该文档说onReceive
方法中的Context
参数是接收器在其中运行的Context
。 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: Application.onCreate的文档说明:
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. 如果onCreate在接收者之前被调用,则默认情况下将首先实例化它。
[1] http://developer.android.com/reference/android/app/Application.html#onCreate() [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() : 只需扩展Application并将其类记录在onReceive()中:
Log.d(TAG, "Context is " + context.getClass().getName());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.