将服务绑定到 Android 中的活动 [英] Bind service to activity in Android

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

问题描述

我正在尝试编写一个使用 RTSP 播放流音频的简单媒体播放器.我有一个 GUI 活动和一个执行播放的服务.我的问题是如何最好地在 Activity 和服务之间进行通信(例如,根据玩家状态更新 GUI).

I'm trying to write a simple media player that plays streaming audio using RTSP. I have a GUI-activity and a service that performs the playback. My question is how to best communicate between the activity and the service (e.g. updating the GUI based on the player state).

我知道我可以使用 onBind() 将服务绑定到活动,但如果我理解正确,如果活动被终止,这将停止服务.即使用户退出活动,我也想继续播放.是否有任何标准或首选方法来处理此问题?

I know that I can bind the service to the activity using onBind(), but if I understand correctly this will stop the service if the activity is killed. I want to continue the playback even if the user exits the activity. Is there any standard or preferred way of dealing with this problem?

推荐答案

"如果您使用 startService(..) 启动一个 android Service,该 Service 将保持运行状态,直到您显式调用 stopService(..).系统可以运行服务有两个原因.如果有人调用 Context.startService() 那么系统将检索服务(创建它并在需要时调用它的 onCreate() 方法)然后调用它的 onStartCommand(Intent, int, int) 方法与客户端提供的参数.服务此时将继续运行,直到 Context.stopService()stopSelf() 被调用.请注意,对 Context.startService() 的多次调用不会嵌套(尽管它们确实会导致对 onStartCommand() 的多次相应调用),因此无论它是多少次一旦Context.stopService()stopSelf() 被调用,启动的服务将停止;但是,服务可以使用它们的 stopSelf(int) 方法来确保服务在启动的意图被处理之前不会停止.

"If you start an android Service with startService(..) that Service will remain running until you explicitly invoke stopService(..). There are two reasons that a service can be run by the system. If someone calls Context.startService() then the system will retrieve the service (creating it and calling its onCreate() method if needed) and then call its onStartCommand(Intent, int, int) method with the arguments supplied by the client. The service will at this point continue running until Context.stopService() or stopSelf() is called. Note that multiple calls to Context.startService() do not nest (though they do result in multiple corresponding calls to onStartCommand()), so no matter how many times it is started a service will be stopped once Context.stopService() or stopSelf() is called; however, services can use their stopSelf(int) method to ensure the service is not stopped until started intents have been processed.

客户端还可以使用 Context.bindService() 来获取到服务的持久连接.如果服务尚未运行(同时调用 onCreate()),这同样会创建服务,但不会调用 onStartCommand().客户端将接收服务从其 onBind(Intent) 方法返回的 IBinder 对象,然后允许客户端调用回服务.只要建立连接,服务就会一直运行(无论客户端是否保留对服务的 IBinder 的引用).通常返回的 IBinder 是针对已经用 AIDL 编写的复杂接口.

Clients can also use Context.bindService() to obtain a persistent connection to a service. This likewise creates the service if it is not already running (calling onCreate() while doing so), but does not call onStartCommand(). The client will receive the IBinder object that the service returns from its onBind(Intent) method, allowing the client to then make calls back to the service. The service will remain running as long as the connection is established (whether or not the client retains a reference on the Service's IBinder). Usually the IBinder returned is for a complex interface that has been written in AIDL.

服务既可以启动也可以绑定连接.在这种情况下,只要服务已启动,或者存在一个或多个带有 Context.BIND_AUTO_CREATE 标志的连接,系统就会保持服务运行.一旦这两种情况都不成立,服务的 onDestroy() 方法被调用并且服务被有效地终止.从 onDestroy() 返回后,所有清理(停止线程、取消注册接收器)都应该完成."

A service can be both started and have connections bound to it. In such a case, the system will keep the service running as long as either it is started or there are one or more connections to it with the Context.BIND_AUTO_CREATE flag. Once neither of these situations hold, the Service's onDestroy() method is called and the service is effectively terminated. All cleanup (stopping threads, unregistering receivers) should be complete upon returning from onDestroy()."

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

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