0

I have an activity that is using fragments for layout. When a user clicks a button I am sending files over the internet, this is too heavy for Async and it crashes my app as I need to dismiss a dialog fragment when it is finished executing. Can services accomplish this? I need to use the service to process the files, and send a message back to tell my activity it has completed, or if the user's phone sleeps or they hit their home button, I need to get whether or not the service completed when I get to the activity onResume. Is this possible?

DeveryDay
  • 161
  • 15
  • 1
    you can use broadcast receiver, get your data in service after getting data send your broadcast. in `onResume` of activity register broadcast and in `onPause` method unRegister that – Shayan Pourvatan May 19 '14 at 14:16
  • What happens if the processing within my service ends while my Activity is paused? Will there be some sort of queue that, when my activity calls onResume, and I register the broadcast, I can get the results from? I just need to know the data was sent so I can dismiss the fragment dialog. – DeveryDay May 19 '14 at 16:25

2 Answers2

0

Create a private Class and then in the OnClick function do this :

new SendPictoServerextends().execute();

SendPictoServerextends :

private class SendPictoServerextends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Please wait...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        try {
                        //code to send data to the server

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println(e);
        }



        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        // Dismiss the progress dialog
        if (pDialog.isShowing())
            pDialog.dismiss();
        System.out.println("Mission is done "  );

    }

}
Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
Mohamed ALOUANE
  • 5,349
  • 6
  • 29
  • 60
  • Unfortunately this won't work for me, as it is similar to what I am doing now. There is no guarantee that the activity will be active when I get to onPostExecute, and therefore throws an IllegalSateException (as I have seen time and time again testing this app) when a user does something like hit "Home" or let the phone go to sleep. And I absolutely need the dialog to show everytime the Async has completed. The ProgressDialog will send my Activity into the background and call it's onPause, causing the exception mentioned above. – DeveryDay May 19 '14 at 14:14
  • When the dialog is pop up ; the user can't do anything even the back button doesn't working while send the datas – Mohamed ALOUANE May 19 '14 at 14:17
  • 1
    For a progress dialog? If it is cancelable it can be just tapped out of. This still doesn't deal with the issue of a user hitting their home button. But even then, the progress dialog itself will put the activity into the background, so there is no guarantee onSaveInstance will be called after we hit onPostExecute. When that happens, the exception I mentioned will be thrown. – DeveryDay May 19 '14 at 14:19
  • Aha; Now I understand what r u trying to say ... check this http://stackoverflow.com/questions/4300291/example-communication-between-activity-and-service-using-messaging?rq=1 – Mohamed ALOUANE May 19 '14 at 14:25
  • @MedAL This is a good starting point but note that the code in the link binds the service to the Activity. So if the Activity is destroyed the service will stop. This is not what OP is looking for. – jacobhyphenated May 19 '14 at 14:33
0

You are correct that Services can handle this. You will need a binder or a broadcast receiver to communicate to your Activity. In my opinion, this can get a little complicated, since you need a way to cache the result until your Activity comes back from the background/is recreated.

You could check out RoboSpice which is a library designed to help solve exactly this problem using Services.

An alternative solution to Android Services would be to create your own manager to handle this. The manager is separate from the Activity lifecycle and should probably be a singleton (Although it does not necessarily have to be), and it can process in a background thread and store the result. In your Fragment, onResume registers with your manager as a listener to receive the results when the operation completes. The Fragment unregistered in onPause to avoid context leaks. Because the manager stores the result, it can hold onto the result until the Fragment resumes and then deliver it.

Rolling your own "Manager" type class is going to be some extra effort on your part, but it might be worth it if you don't want make extensive use of third party libraries for whatever reason.

jacobhyphenated
  • 2,105
  • 2
  • 17
  • 27
  • Is there a way for the service to run even if the activity is paused, and then have my activity look at the service and see whether it completed when I call onResume, or is it more complicated than that? – DeveryDay May 19 '14 at 14:38
  • I believe it is a little more complicated than that. The service is really only supposed to run until its task is complete. However, you can create long running services, you just have to watch out for the service lifecycle as well as the Activity lifecycle. To guarantee that a service keeps running, you can use a `Foreground Service`, but that requires you to place a notification in the status bar and is probably not what you are looking for. – jacobhyphenated May 19 '14 at 16:33