简体   繁体   中英

Android Send data from IntentService to a bound Activity

I created an android app with an intent service bound to an activity. I followed this excellent article .

But now I would like to send data from my IntentService to update my view (I mean, my activity).

The context: I want to create a simple countdown. The service send the "time" to the activity every minute or second for example.

How can I send data from my IntentService to my Activity ?

In kotlin please... Thank you!

In Android there are many APIs which allow us to send messages from a service to an activity. Which API we should use that depends on whether the service and the activity are running in the same process or not.

If they are running in the same process, then you have 2 options

  • Using local broadcasts with LocalBroadcastManager . It allows only components in an app communicate with other. It is more secure and efficient than sending a global broadcast through the system.
  • Using EventBus library if you don't want to implement broadcast receiver.

If they are running in different processes, then you have 3 options

  • Using Messenger (is coupled with a Handler ), messages will be processed on a single thread by the Handler sequentially.
  • Using AIDL interface, the difference between Messenger and AIDL is AIDL can execute tasks on binder threads concurrently.
  • Using global broadcasts with BroadcastReceiver .

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