How to get results from an IntentService back into an Activity?

前端 未结 4 959
猫巷女王i
猫巷女王i 2020-12-01 00:46

I am using an IntentService to handle network communications with a server via JSON. The JSON/server part is working fine, but I\'m having trouble getting the results back

4条回答
  •  一向
    一向 (楼主)
    2020-12-01 01:15

    There's many ways to do stuffs in background getting back a result (AsyncTask, bind an Activity to a Service...), but if you want to mantain your IntentService's code you can simply:

    1- send a broadcast intent (that contains the status in its extended data) at the end of the work in your IntentService

    2- implement a LocalBroadcastReceiver in your Activity that consumes the data from the Intent

    This is also the recommended way in the official documentation (if you want to mantain your IntentService):

    https://developer.android.com/training/run-background-service/report-status.html

提交回复
热议问题