如何使用服务更新的Android SQLite数据库
问题描述:
我能够通过从我的活动线程更新SQLite数据库和检索数据。但我需要用更新服务,这将在每5分钟详情更新数据库,无论是我的应用程序运行与否的数据库。因此,当我运行应用程序,我会从当前数据库中获取数据。
我坚持在写服务更新database.Can有人指导。
I am able to update sqlite database and retrieving the data through a thread from my Activity. But i need to update the database using Service, which will update the database in every 5 minute, whether my Application running or not. So when i run the application, i will get data from current database. I am stuck while writing Service to update the database.Can someone guide.
答
这是您的服务类创建一个定时器对象定时器定时器=新定时器();
结果
在上创建
Create a timer object on your service class
Timer timer=new Timer();
On on create
@Override
public void onCreate()
{
super.onCreate();
timer.schedule(new DelayedTask(),300, 300000);// 300000 is 5min
}
类DelayedTask的
Class DelayedTask
private class DelayedTask extends TimerTask
{
@Override
public void run() {
Log.i(tag,"Timer Task executing");
// code for updating sqlite database
}
}