Home:ALL Converter>Android: update UI depending on value of object from background thread in service

Android: update UI depending on value of object from background thread in service

Ask Time:2016-03-01T03:58:19         Author:user

Json Formatter

I am currently developing an Android app which contains the following activities:

  • MainActivity;
  • UpdateActivity.

Besides that it also contains a completely separate package in which exists the following classes:

  • SyncService: which is, as the name already indicates, a service. To be more specific a bound service;
  • SyncServiceManager: a singleton object which binds to the service.
  • SyncTask: only containing the logic that the service needs to do. So the service is the only object that interact with it;
  • A couple of interfaces to make it possible for my manager to interact with the service. And 2 interfaces just to use the Observer Pattern for my app. The Service is the subject which is supposed to notify the manager, the syncTask and the updateActivity to do something.

The problem I am facing is that I want to modify the UI of my updateActivity depending on an enum that changes as the service needs to do his job. Which is notifying the syncTask to do the requested job (Configuring, Resetting or Syncing the application database with the data of a webservice).

However since I want to make the service do his thing in the background I'm using a new thread object in which it can do thing I want it to do. This is all kind of working the way it should, except for the fact that I can modify the UI in the update activity because the UI can only be edited from the UI Thread. I've already found a very promising link on the forum, but I just can't figure out if it really is the thing I need and/or how to implement it. android: update UI from another thread in another class

This is my code. The Service:

public enum ServiceState {
    RESETTING,
    CONFIGURING,
    SYNCHRONIZING,
    IDLE
}

private Vector<IObserver> observers = new Vector<IObserver>();
private IBinder syncBinder = new SyncServiceBinder();
private ServiceState serviceState;

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return syncServiceBinder;
}

private final IBinder syncServiceBinder = new SyncServiceBinder();

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

    Thread th = new Thread(new Runnable() {
        @Override
        public void run() {
            //Modifying the service state and starting the sync depending on the state.
            startSync();
        }
    });

    th.start();

    return START_REDELIVER_INTENT;
}

private void startSync() {
    notifyObservers(serviceState);
}

@Override
public void registerObserver(IObserver iObserver) {
    observers.add(iObserver);
}

@Override
public void unregisterObserver(IObserver iObserver) {
    observers.remove(iObserver);
}

@Override
public void notifyObservers(SyncService.ServiceState serviceState) {
    for (IObserver observer : observers) {
        observer.update();
    }
}

The update activity:

public class UpdateActivity extends Activity implements IObserver {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.update_activity);
}

@Override
public void update() {
    //Modify UI from here depeding on the serviceState
}

}

I've also thought of using the runOnUiThread function which comes with the activity class, but it just gets fired way too late. The same thing with creating a new handler within the updateactivity and create a runnable which should allow me to send a message to the handler.

You guys have any thoughts/suggestions?

Thanks in Advance,

Michael

Author:user,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/35708473/android-update-ui-depending-on-value-of-object-from-background-thread-in-servic
UMESH0492 :

Broadcast Listener would be best in this case and also recommended. Just create Broadcast listener in activity which will update the UI, register it in onStart and unregister in onStop.",
2016-02-29T20:21:31
yy