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.
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.
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.