[英]Android Send data from IntentService to a bound Activity
I created an android app with an intent service bound to an activity. 我创建了一个将意图服务绑定到活动的android应用。 I followed this excellent article .
我关注了这篇出色的文章 。
But now I would like to send data from my IntentService to update my view (I mean, my activity). 但是现在我想从我的IntentService发送数据以更新我的视图(我的意思是我的活动)。
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 ? 如何将数据从IntentService发送到Activity?
In kotlin please... Thank you! 请在科特林...谢谢!
In Android there are many APIs which allow us to send messages from a service to an activity. 在Android中,有许多API允许我们将消息从服务发送到活动。 Which API we should use that depends on whether the service and the activity are running in the same process or not.
我们应该使用哪个API取决于服务和活动是否在同一进程中运行。
If they are running in the same process, then you have 2 options 如果它们在同一进程中运行,那么您有2个选择
If they are running in different processes, then you have 3 options 如果它们在不同的进程中运行,那么您有3个选择
Handler
sequentially. Handler
。 Messenger
and AIDL
is AIDL
can execute tasks on binder threads concurrently. Messenger
和AIDL
之间的区别在于AIDL
可以在活页夹线程上同时执行任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.