I've declared a Service class:
public class MyService extends Service{
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new Parsing().execute();
return Service.START_STICKY;
}
private class Parsing extends AsyncTask<Void, Void, Void> {
List<MyObject> myList = null;
MyAdapter adapter = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
myList = new ArrayList<MyObject>();
}
@Override
protected Void doInBackground(Void... params) {
while (downloadDataFromInternet)
myList.add(new MyObject(par1, par2, par3, par4));
}
@Override
protected void onPostExecute(Void result) {
if (myList.size() > 0)
adapter = new MyAdapter(this, myList);
}
}
}
Now I would like to execute this service every 10 minutes (for example) also when activity is in background, but I want that when activity come back to foreground, listView of MyFragment uses the adapter declared in the service. Can you please help me? I have no idea about how to do this.
You can use Timer Task to execute some task after a specific time period
1 You need to create Subclass of TimerTask
eg
2 Start this task from your service
Hope this is what you want