通过RECEIVE_BOOT_COMPLETED一个BroadcastReceiver访问数据库 [英] Accessing a database through a broadcastreceiver on RECEIVE_BOOT_COMPLETED

查看:226
本文介绍了通过RECEIVE_BOOT_COMPLETED一个BroadcastReceiver访问数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当手机完成开机我想自动重新注册了一些报警,根据小时,从一个数据库,分钟+信息来源。

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

我尝试用一​​个broadcastreveiver做到这一点,但它不会工作。 尝试在启动时访问该数据库(DB)的辅助类时崩溃。通过应用程序访问它时,数据库的辅助类工作正常。

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?

推荐答案

您不想做数据库I / O在任何清单注册的广播接收器。你不知道需要多长时间(基于其他设备的活动),和你的的onReceive()方法是在主应用程序线程上运行,所以你的时间是有限的,你的CPU占用很大。

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.

请把控制权交给一个 IntentService 做数据库I / O和安排你的报警。该 IntentService 会打电话给你的 onHandleIntent()在后台线程,这样你就可以把你所需要的时间,服务会自动消失,当 onHandleIntent()完成。

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.

这篇关于通过RECEIVE_BOOT_COMPLETED一个BroadcastReceiver访问数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆