Naba
Naba

Reputation: 513

How to use Service to update sqlite database in android

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.

Upvotes: 1

Views: 5128

Answers (2)

Sandeep Kumar P K
Sandeep Kumar P K

Reputation: 7482

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


    }

}

Upvotes: 2

spatulamania
spatulamania

Reputation: 6663

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.

Upvotes: 0

Related Questions