简体   繁体   English

在BroadcastReceiver.onReceive(..)之前是否会调用Application.onCreate(Bundle)?

[英]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.

相关问题 BroadCastReceiver.onReceive 是如何调用的? - How is BroadCastReceiver.onReceive called? 在Application.onCreate()上的BroadcastReceiver - BroadcastReceiver on Application.onCreate() BroadcastReceiver onReceive 方法在 Fragment onCreate() 之前调用 - BroadcastReceiver onReceive method called before Fragment onCreate() 在Application.onCreate代码之前调用NotificationListenerService - NotificationListenerService called before Application.onCreate code 没有为PHONE_STATE调用BroadcastReceiver.onReceive - BroadcastReceiver.onReceive is not called for PHONE_STATE Android Activity::onCreate 在 Application.onCreate 之前调用 - Android Activity::onCreate called before Application.onCreate BroadcastReceiver.onReceive 是否每条短信只调用一次? - Is the BroadcastReceiver.onReceive called exactly once per sms? BroadcastReceiver.onReceive没有被调用,但Activity.onNewIntent做了。 为什么? - BroadcastReceiver.onReceive not called, but Activity.onNewIntent do. Why? 对user_present调用BroadcastReceiver.onReceive两次 - BroadcastReceiver.onReceive is called twice for user_present 是否有可能知道我的 Application.onCreate() 是因为 Activity 启动还是因为 BroadcastReceiver 而被调用的? - Is it possible to know if my Application.onCreate() was called because of an Activity starting or because of a BroadcastReceiver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM