0

How to continuously get the results from a service to UI or MainActivity.?

I am copying some large file using a service,and i want to update the progress bar in Foreground.

Hit4man47
  • 23
  • 4
  • See [link](https://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog?rq=1) – Fatih Santalu Jul 15 '17 at 22:00

1 Answers1

0

Simplest way to do this is to use Singleton object with listener. Subscribe for update in activity and just set values in Service.

public class ProgressSingleton {
    private static ProgressSingleton instance;

    private ProgressListener progressListener = null;

    public static void initInstance() {
        if (instance == null) {
            instance = new ProgressSingleton();
        }
    }

    public static ProgressSingleton getInstance() {
        return instance;
    }

    private ProgressSingleton() {
    }

    public void setProgress(int progress) {
        if (progressListener != null) {
            progressListener.onProgressUpdated(progress);
        }
    }

    public interface ProgressListener {
        void onProgressUpdated(int progress);
    }
}

Make shure that you handle updates from listener in UI thread. Also you can start your service in foreground, best option for big files.