Most visited

Recently visited

Added in API level 1

RemoteCallbackList

public class RemoteCallbackList
extends Object

java.lang.Object
   ↳ android.os.RemoteCallbackList<E extends android.os.IInterface>


负责维护远程接口列表的繁重工作,通常用于从客户端向Service执行回调。 特别是,这个:

要使用此类,只需创建一个实例和服务,然后将其register(E)unregister(E)方法作为客户端注册并取消注册您的服务。 要在回调到注册客户端,使用beginBroadcast()getBroadcastItem(int) ,并finishBroadcast()

如果注册回调的过程消失,该课程将自动从列表中删除它。 如果你想在这种情况下做额外的工作,你可以创建一个实现onCallbackDied(E)方法的子类。

Summary

Public constructors

RemoteCallbackList()

Public methods

int beginBroadcast()

准备开始调用当前注册的回调。

void finishBroadcast()

通过调用 beginBroadcast()以前启动的广播状态。

Object getBroadcastCookie(int index)

检索与由 getBroadcastItem(int)返回的项目关联的cookie。

E getBroadcastItem(int index)

检索之前以 beginBroadcast()开始的活动广播中的项目。

int getRegisteredCallbackCount()

返回已注册回调的数量。

void kill()

禁用此回调列表。

void onCallbackDied(E callback, Object cookie)

当列表中的回调进程已经消失时调用。

void onCallbackDied(E callback)

旧版本 onCallbackDied(E, Object)不提供Cookie。

boolean register(E callback, Object cookie)

向列表中添加一个新的回调。

boolean register(E callback)

简单版本的 register(E, Object)不包含cookie对象。

boolean unregister(E callback)

从列表中删除以前使用 register(E)添加的回调。

Inherited methods

From class java.lang.Object

Public constructors

RemoteCallbackList

Added in API level 1
RemoteCallbackList ()

Public methods

beginBroadcast

Added in API level 1
int beginBroadcast ()

准备开始调用当前注册的回调。 这将创建回调列表的副本,您可以使用getBroadcastItem(int)检索项目。 请注意,一次只能有一个广播活动,因此您必须始终从同一个线索调用此广播(通常通过安排Handler )或进行自己的同步。 完成后您必须致电finishBroadcast()

传递广播的典型循环如下所示:

 int i = callbacks.beginBroadcast();
 while (i > 0) {
     i--;
     try {
         callbacks.getBroadcastItem(i).somethingHappened();
     } catch (RemoteException e) {
         // The RemoteCallbackList will take care of removing
         // the dead object for us.
     }
 }
 callbacks.finishBroadcast();

Returns
int Returns the number of callbacks in the broadcast, to be used with getBroadcastItem(int) to determine the range of indices you can supply.

也可以看看:

finishBroadcast

Added in API level 1
void finishBroadcast ()

通过拨打beginBroadcast()以前启动的广播状态。 当您完成广播时,必须始终调用它。

也可以看看:

getBroadcastCookie

Added in API level 4
Object getBroadcastCookie (int index)

检索与由 getBroadcastItem(int)返回的项目相关的cookie。

Parameters
index int
Returns
Object

也可以看看:

getBroadcastItem

Added in API level 1
E getBroadcastItem (int index)

检索之前以beginBroadcast()开始的活动广播中的项目。 只能在广播开始后才能调用,调用finishBroadcast()后其数据不再有效。

请注意,在调用返回的对象之前,其中一个返回的回调过程可能会消失,因此在调用返回的对象时需要捕获RemoteException 但是,回调列表本身会在它检测到它不再有效时处理注销这些对象,因此您可以通过忽略它来处理这样的异常。

Parameters
index int: Which of the registered callbacks you would like to retrieve. Ranges from 0 to 1-beginBroadcast().
Returns
E Returns the callback interface that you can call. This will always be non-null.

也可以看看:

getRegisteredCallbackCount

Added in API level 17
int getRegisteredCallbackCount ()

返回已注册回调的数量。 请注意,已注册的回调数可能与beginBroadcast()返回的值不同,因为前者返回通话时注册的回调数,第二个则返回广播的回调数。

如果需要做一些本来不会执行的工作,该功能对决定是否安排广播很有用。

Returns
int The size.

kill

Added in API level 1
void kill ()

禁用此回调列表。 所有注册的回调register(E)注册,并且该列表被禁用,以便将来拨打register(E)将失败。 这应该在服务停止时使用,以防止客户在停止后注册回调。

也可以看看:

onCallbackDied

Added in API level 4
void onCallbackDied (E callback, 
                Object cookie)

当列表中的回调进程已经消失时调用。 为了向后兼容,默认实现调用onCallbackDied(E)

Parameters
callback E: The callback whose process has died. Note that, since its process has died, you can not make any calls on to this interface. You can, however, retrieve its IBinder and compare it with another IBinder to see if it is the same object.
cookie Object: The cookie object original provided to register(E, Object).

也可以看看:

onCallbackDied

Added in API level 1
void onCallbackDied (E callback)

旧版本 onCallbackDied(E, Object)不提供Cookie。

Parameters
callback E

register

Added in API level 4
boolean register (E callback, 
                Object cookie)

向列表中添加一个新的回调。 此回调将保留在列表中,直至unregister(E)或其主持过程的相应呼叫消失。 如果回调已经注册(通过检查callback.asBinder()对象是否已经在列表中确定),则它将保持原样。 注册不算在内; 拨打unregister(E)的单个电话将在任何号码呼叫注册之后删除回拨。

Parameters
callback E: The callback interface to be added to the list. Must not be null -- passing null here will cause a NullPointerException. Most services will want to check for null before calling this with an object given from a client, so that clients can't crash the service with bad data.
cookie Object: Optional additional data to be associated with this callback.
Returns
boolean Returns true if the callback was successfully added to the list. Returns false if it was not added, either because kill() had previously been called or the callback's process has gone away.

也可以看看:

register

Added in API level 1
boolean register (E callback)

简单版本的 register(E, Object)不需要cookie对象。

Parameters
callback E
Returns
boolean

unregister

Added in API level 1
boolean unregister (E callback)

从列表中删除以前使用register(E)添加的回调。 这使用callback.asBinder()对象来正确查找以前的注册。 注册不算在内; 在任何号码呼叫register(E)之后,单个注销呼叫将删除回拨。

Parameters
callback E: The callback to be removed from the list. Passing null here will cause a NullPointerException, so you will generally want to check for null before calling.
Returns
boolean Returns true if the callback was found and unregistered. Returns false if the given callback was not found on the list.

也可以看看:

Hooray!