Skip to main content

How to adapt basic android service to run an activity in the background?



I want to create an android service which is able to fetch and parse JSON data into my local database. I have already written the code for this to be done as a normal activity. But I would like to change it so that it updates the database on a regular basis. How would I go about doing this?





So far I have created a basic service but I do not know how to tie in my activity into the service or how to set the service to start up when the application is opened up.





Basic Data Service







public class DataFetcher extends Service {

private static final int POLL_PERIOD=120000;

private AtomicBoolean active=new AtomicBoolean(true);



@Override

public void onCreate() {

super.onCreate();



}



@Override

public IBinder onBind(Intent intent) {

return(null);

}



@Override

public void onDestroy() {

super.onDestroy();

}



private Runnable threadBody=new Runnable() {

public void run() {

while(active.get()) {



SystemClock.sleep(POLL_PERIOD);

}

}

};

}




Comments

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?