简体   繁体   中英

Is it possible to implement a BroadcastReceiver as a singleton if it's to be registered in the manifest?

I'm creating a BroadcastReceiver which is to be registered under as an application component in the manifest.

I'd like to ensure that there is only one instance of this class. Is it possible to enforce a singleton pattern for this class? Making the constructor private in this case doesn't work because the system is then unable to instantiate it, and returns a NullPointerException.

Is it possible to enforce a singleton pattern for this class?

No, sorry. The framework will create one instance per received broadcast, and that is not changeable, given that you are registering for it in the manifest.

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