如何将两个Android活动绑定到一项服务? [英] How to bind two Android Activities to one Service?

查看:146
本文介绍了如何将两个Android活动绑定到一项服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想举个例子,其中两个不同的活动(第一个活动中的按钮打开第二个活动)正在与一个服务(AIDL等)进行通信 我尝试了许多不同的教程,但它们仅涉及如何进行一项活动->一种服务.

I would like to ask for some example, where two different activities (button in first activity open second activity), are communicating with one service (AIDL, etc.) I've tried many different tutorials, but they are only about how to make one activity -> one service.

推荐答案

这可能已经很老了,但我还是会尽力回答...

This is probably old, but I'll try to answer it anyways...

在Android中,一次只能看到一个Activity可以绑定到一个Service,并且一次只能显示一个Activity,所以没有真正的理由想要一次绑定两个Activity.时间.

In Android, seeing as only one Activity can bind to a Service at a time, and only one Activity can be shown at a time, there is no real reason to want to bind 2 Activities at a time.

但是,如果您愿意,最好的解决方案是在onResume()方法中绑定Service,并在onPause()方法中取消绑定.这样,您就可以为两个不相关的活动"授予对该服务的访问权限,而一次只能绑定一个.

But, if you'd like, the best solution is to bind the Service in the onResume() method, and unbind it in the onPause() method. This allows you to give two un-related Activities access to the service, while only having one bound at a time.

这篇关于如何将两个Android活动绑定到一项服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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