Application class 是否保证在定义的引导接收器被调用之前被实例化

Is the Application class guaranteed to be instantiated before a defined boot receiver is called

请问这么初级的问题。我知道应用程序 class 在我的应用程序进程启动时实例化,我知道当 phone 完成启动时将调用我的引导接收器。我假设因为 phone 通过清单知道我的应用程序拥有 BOOT_COMPLETED 意图过滤器,所以重启过程是。 Phone 重新启动,phone 使用 BOOT_COMPLETED 启动所有进程,phone 触发 BOOT_COMPLETED 广播。我的担心来自于想知道我是否在我的启动接收器中引用了应用程序 class 实例变量,如果接收器在我的应用程序 class 被实例化之前被调用。

如果这很明显,请再次原谅。我从来没有完全理解重启机制。

Phone reboots, phone starts all processes with BOOT_COMPLETED, phone fires off BOOT_COMPLETED broadcast.

我更愿意将其表述为 "phone reboots, phone fires off BOOT_COMPLETED broadcast, and normal broadcast processing occurs, including starting any necessary processes"。

My concern came from wondering if I reference Application class instance variables within my boot receiver if the receiver would ever get called before my Application class was instantiated.

不应该。实例化的顺序应该是:

  • 您在清单中定义的任何 ContentProviders,然后

  • Application实例,然后

  • 触发流程需求的组件(在本例中,您的 ACTION_BOOT_COMPLETED BroadcastReceiver

应用程序总是在它的任何 Activities/Services/Receivers 之前启动。以下是一些详细介绍的博客:

[已编辑]

但是,根据@CommansWare 的评论:

Based on logging, the instance of the ContentProvider is created after the instance of the Application. However, onCreate() of the ContentProvider is called before onCreate() of the Application.

因此,尝试在提供商的 onCreate().

中使用 Application 实例可能不安全