1

Do You know if there is any way to start a Service from the MainActivity, send some data and when the service is done, get the data from the Service. For example:

//MainActivity.class

private ArrayList myList = new ArrayList();

public void fetchDataFromInternet()
{
    Intent intent = new Intent(this, DataFromInternetService.class);
    intent.putArrayListExtra("MYLIST", myList);  
    startService(intent);

    //In DataFromInternetService the app fetches data from the internet and puts them in the myList.
    /*
         TODO:
         1. Get the data from the service after the it has finished.
    */
}
//Note: The service runs only 1 time.

Also, If this is not possible, what alternative do You suggest, in which MainActivity has access to the data from the DataFromInternetService.class.

Note: I don't want to use LoaderManager, I use Services to do background tasks that don't effect the UI.

manfcas
  • 1,933
  • 7
  • 28
  • 47
LazyAnalyst
  • 426
  • 3
  • 16
  • 1
    you can use local broadcast receiver. – Junaid Hafeez Mar 01 '17 at 20:47
  • Yeah but how I am going to get the new "myList" after the service has finished? – LazyAnalyst Mar 01 '17 at 20:49
  • what do you mean by on finishing service? well you can send list via intent from broadcast receiver. – Junaid Hafeez Mar 01 '17 at 20:51
  • the service runs in the background until it finishes getting all the data... I could use a broadcast receiver...However I want to return "myList" in the MainActivity, specifically in fetchDataFromInternet() method, so that I can assign it to myList later on – LazyAnalyst Mar 01 '17 at 20:57

4 Answers4

1

To communicate with your service you can use Bound Service:

Please check this: https://developer.android.com/guide/components/bound-services.html

or you can send your data within: broadcast receiver, database, shared preferences. It depends on your needs.

Bro
  • 53
  • 6
0

Instead of using a service to run stuff on the background you should use Async Tasks

you can do the following:

new AsyncTask<Void,Void,Void>(){

doInBackground(){

//DO STUFF

}

onPostExecute(){
//Do What You want with the results
}
}

}
Amr Yousef
  • 554
  • 1
  • 10
  • 27
  • Sure, I could use a LoaderManagerwith an AsyncTask as well to prevent any "zombie" tasks if the user navigates away from the app, however my instructor told me to use LoaderManager only If I have to update the UI later on. If the background tasks don't affect the UI, i Should use Services – LazyAnalyst Mar 01 '17 at 21:02
0

If you want to communicate with your service you have to use bound service and bind to it in your activity. Then create handlers for your activity and also for your service. Using these handlers you can notify your activity once the download is finished. Please let me know if you managed to solve your problem or some further instructions are needed.

0

That's the kind of question where any discussion at this point was already finished few years ago, but still bothers us every time we need to choose the most comfortable approach for our app. I don't thing that I should dive into code samples or low level explanations. You can find more than needed information by yourself.
One thing I can be helpful(I hope so) is to share my approach. When I'm facing issues like how to pass data between two different components? - I treat such situations kind of straight-forward. You need to notify your hungry component(activity in our particular case) that's fetching was finished an it can get his info from a trustworthy place. Or to be more cruel, pass the data directly to it. This can be achieved by different implementations/solutions but the abstract ideology remains the same.
What can you do? Here are some links :

And still there are even more valuable solutions, but those are enough.

Community
  • 1
  • 1
Yurii Tsap
  • 3,554
  • 3
  • 24
  • 33