How to use Service to update sqlite database in android

左心房为你撑大大i 提交于 2019-12-11 05:15:04

问题


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

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