绑定服务,在Android的活动绑定、Android

由网友(白头之吟)分享简介:我试着写一个简单的媒体播放器,可以播放使用RTSP流媒体音频。我有一个GUI活性和执行再现的服务。我的问题是如何以最佳的活动和服务之间的通信(例如更新根据玩家的状态GUI)。I'm trying to write a simple media player that plays streaming audio usi...

我试着写一个简单的媒体播放器,可以播放使用RTSP流媒体音频。我有一个GUI活性和执行再现的服务。我的问题是如何以最佳的活动和服务之间的通信(例如更新根据玩家的状态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()服务绑定到该活动,但如果我理解正确的话,这将停止该服务,如果该活动被杀害。我想继续播放,即使用户退出的活性。是否有处理这个问题的任何标准或preferred方式?

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?

推荐答案

如果你开始一个Android服务与 startService(..)该服务将继续运行,直到您显式调用 stopService(..)。 有一个服务可以由系统运行两个原因。如果有人致电 Context.startService()则系统将检索服务(创建它,并调用它的的onCreate()方法,如果需要的话),然后调用它的 onStartCommand(意向,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()。客户端将收到的IBinder 对象从 onBind(意向)方法的服务回报,让客户再拨打电话回服务。只要建立连接(客户端是否不保留在参考服务的的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()."

阅读全文

相关推荐

最新文章