Jignesh Ansodariya
Jignesh Ansodariya

Reputation: 12695

android:How to get call backs from service to activity?

I am using a service to upload a file to server and I am getting some result after file uploaded I want some call backs from service like when upload completed, when got result, if didn't got result in specific time, when network lost etc. as per these call backs I need changes in my activity from where my service was called. currently I am using different-different broadcast send and receive like this.

Intent w = new Intent("<KEY>");
w.putExtra("***", ***);
sendBroadcast(w);

It is working fine now but I want to know that it is proper way to do such or is there any better way? I also red about pass Handler from activity and pass message queue from service but I am not comfortable this.

Upvotes: 3

Views: 146

Answers (1)

type-a1pha
type-a1pha

Reputation: 1893

Using BroadcastReceivers is perfectly fine. But if your worker upload-thread needs to run once-off time, then an AsyncTask may better suits your needs. You do your work in doInBackground and then use onPostExecution() to update your GUI.

As you said, you can also use a Handler inside your Activity. But then your Service will need to be a bound Service in order for you to be able to pass the Handler to the Service.

For very simple things, I'd advise you to use an AsyncTask.

Upvotes: 1

Related Questions