[英]Will Application.onCreate(Bundle) be called before BroadcastReceiver.onReceive(..)?
Just wanted to verify that Application.onCreate()
is guaranteed to be called prior to BroadcastReceiver.onReceive()
? 只是想验证是否保证在
BroadcastReceiver.onReceive()
之前调用Application.onCreate()
BroadcastReceiver.onReceive()
? Let say you are waiting for BOOT broadcast or SMS broadcast, can you be sure that Application.onCreate()
has already been called once before you reach BroadcastReceiver.onReceive()
? 假设您正在等待BOOT广播或短信广播,您是否可以确定在到达
BroadcastReceiver.onReceive()
之前已经调用过Application.onCreate()
一次? Thanks 谢谢
The docs tell us the following: 文档告诉我们以下内容:
public void onCreate ()
public void onCreate()
Called when the application is starting, before any activity, service, or receiver objects (excluding content providers) have been created.
在应用程序启动时调用, 在创建任何活动,服务或接收方对象(不包括内容提供程序) 之前调用。
Found here: http://developer.android.com/reference/android/app/Application.html 在此处找到: http : //developer.android.com/reference/android/app/Application.html
public void onReceive(Context context, Intent intent)
如果注册静态接收器,则上下文是app,否则它是您调用registerReceiver
的上下文
Old question, however I can provide an updated answer and actual test results. 老问题,但我可以提供更新的答案和实际的测试结果。
Originally I assumed so and set my application onCreate() to save app context first, like so: 最初我假设如此,并将我的应用程序设置为onCreate()以首先保存应用程序上下文,如下所示:
@Override
public void onCreate()
{
myapp.app_context = this;
super.onCreate();
Now I got a receiver declared so: 现在我有一个接收器声明:
<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 in on receive: 在收到:
@Override
public void onReceive(Context context, Intent intent)
{
ctx = myapp.app_context;
Got a stack trace showing ctx (myapp.app_context) is NULL! 有一个堆栈跟踪显示ctx(myapp.app_context)为NULL! Got 1 on an Android 6 Galaxy J7 device and 1 on an Android 9 Xperia XZ1.
在Android 6 Galaxy J7设备上获得1,在Android 9 Xperia XZ1上获得1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.