Update UI with background service

1.9k views Asked by At

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.

4

There are 4 answers

0
Gaurav On

You can use Timer Task to execute some task after a specific time period

1 You need to create Subclass of TimerTask

eg

class Mytask extends Timertask{
 @Override
public void run() {

    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
             //your code you want to run in UI thread
         });
     }
}

2 Start this task from your service

                Timer timer = new Timer();
                MyTask task= new MyTask(getApplicationContext());
                timer.scheduleAtFixedRate(task,1,1000*60*10); // for 10 min

Hope this is what you want

2
Prathmesh Swaroop On

If you have to exactly execute the service in 10 minutes of interval . You can use thread scheduler as :

private final ScheduledExecutorService scheduler =
    Executors.newScheduledThreadPool(1);



@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    //schedule a thread
    scheduler.schedule(new Runnable() {
        public void run() { 
            startservice();
        }
    }, 10, TimeUnit.MINUTES);

    return Service.START_STICKY;
}

private startService(){
    // you background service related work

    while (downloadDataFromInternet)
        myList.add(new MyObject(par1, par2, par3, par4));

    // schedule a new thread
    scheduler.schedule(new Runnable() {
        public void run() { 
            startservice();
        }
    }, 10, TimeUnit.MINUTES);
}
2
Amsheer On

My suggestion use database:

Once this Background task runs then update your data into database. Once you open your UI then you can read value from database and you can also update UI.

0
Simas On

Sounds like you want to communicate between the Service and the Activity. While service is like independent from a specific context, activity isn't.

Therefore Service cannot (should not) talk to the activity directly, especially if the activity is killed/re-created.

What you should do is develop a method of intercommunicating by using a database or SharedPreferences, to store your data from within the service. Then, the activity would fetch this data and use it.