简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM