Android体系结构组件-Activity/Fragment和Service/IntentService之间的通信 [英] Android Architecture Components - communication between Activity/Fragment and Service/IntentService

查看:128
本文介绍了Android体系结构组件-Activity/Fragment和Service/IntentService之间的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提前谢谢!

解决方案

您可以使用生命周期感知的AndroidX组件(与ViewModel一起使用)代替IntentService.该组件称为 WorkManager : https://codelabs.developers.google.com/codelabs/android-workmanager/#0

在您的其他S.O中提出了另一种替代方法(更具挑战性).帖子:使用RxJava Observable/Flowable/Processor将数据从IntentService推送到ViewModel.

The answer to this question is providing me with a marvelous guide to how to use services in the Android Architecture Components/Jetpack environment.

It suggests that IntentServices should just plug into ViewModels via Repositories, as if they were any other data source, like a web service.

But neither that answer nor the Jetpack guide upon which it's based have much information about the so-called "Remote Data Source" object that would start and bind to the service, observe its LiveData and cascade it back up to the Repository.

What is it? An ordinary Java object that gets injected into the Repository? A singleton? Some special Lifecycle-aware subclass?

Where does it get the context it needs to start the service?

Is there example code you can point me to? If not, could you just sketch out the basics of what that would look like, including how its lifecycle would be linked to the lifecycle of the service itself, for newbies like me?

Thanks in advance!

解决方案

Instead of an IntentService, you can use an AndroidX component which is lifecycle-aware (works with ViewModel). This component is called WorkManager: https://codelabs.developers.google.com/codelabs/android-workmanager/#0

Another alternative (more challenging) was suggested in your other S.O. post: Use RxJava Observable/Flowable/Processor to push data from IntentService to ViewModel.

这篇关于Android体系结构组件-Activity/Fragment和Service/IntentService之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆