问题
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.
回答1:
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
}
Class DelayedTask
private class DelayedTask extends TimerTask
{
@Override
public void run() {
Log.i(tag,"Timer Task executing");
// code for updating sqlite database
}
}
回答2:
This question if very general. To see how to use a service, take at look at this.
If you want your service to be woken every five minutes, you should use the AlarmManager.
If you have a specific question along the way, post that.
来源:https://stackoverflow.com/questions/7721800/how-to-use-service-to-update-sqlite-database-in-android