Accessing a database through a broadcastreceiver on RECEIVE_BOOT_COMPLETED

半世苍凉 提交于 2019-12-01 07:24:25

问题


When the phone is finish booting up I want to automatically re-register some alarms, based on hour, minute ++ infomation from a database.

I try to do this with a broadcastreveiver, but it won't work. It crashes when trying to accessing the database(DB) helper class on boot up. The DB helper class works fine when accessing it through the app.

Is this possible or do I have to use some alternative solution like a service?


回答1:


You do not want to do database I/O in any manifest-registered broadcast receiver. You do not know how long it will take (based on other device activity), and your onReceive() method is running on the main application thread, so your time is limited and your CPU footprint is large.

Please pass control to an IntentService for doing the database I/O and scheduling your alarms. The IntentService will call your onHandleIntent() on a background thread, so you can take the time you need, and the service automatically goes away when onHandleIntent() completes.



来源:https://stackoverflow.com/questions/8261577/accessing-a-database-through-a-broadcastreceiver-on-receive-boot-completed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!