codecamp

鸿蒙OS RemoteObject

RemoteObject

java.lang.Object

|---ohos.rpc.RemoteObject

public class RemoteObject
extends Object
implements IRemoteObject

实现远程对象。

服务提供者必须继承这个类。

如果 RemoteObject 定义为匿名类、本地类或成员类而不是静态单例类,则可能会发生潜在泄漏。

嵌套类摘要

从接口 ohos.rpc.IRemoteObject 继承的嵌套类/接口
IRemoteObject.DeathRecipient

字段摘要

从接口 ohos.rpc.IRemoteObject 继承的字段
DUMP_TRANSACTION, INTERFACE_TRANSACTION, MAX_TRANSACTION_ID, MIN_TRANSACTION_ID, PING_TRANSACTION

构造函数摘要

构造函数 描述
RemoteObject(String descriptor) 用于使用默认接口描述符创建 RemoteObject 实例的构造函数。

方法总结

修饰符和类型 方法 描述
boolean addDeathRecipient(IRemoteObject.DeathRecipient recipient, int flags) 注册一个用于接收远程代理死亡通知的回调。
void attachLocalInterface(IRemoteBroker localInterface, String descriptor) 修改当前 RemoteObject 的描述。
protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) 将当前接口的状态转储到具有特定描述符的文件中。
void dump(FileDescriptor fd, String[] args) 将当前接口的状态转储到具有特定描述符的文件中。
protected void finalize() 当垃圾收集确定不再有对该对象的引用时,由对象上的垃圾收集器调用。
int getCallingPid() 获取 RemoteProxy 对象的 PID。
int getCallingUid() 获取 RemoteProxy 对象的 UID。
String getInterfaceDescriptor() 查询接口描述符。
boolean isObjectDead() 检查 RemoteObject 是否已失效。
boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) 设置接收请求的条目。
IRemoteBroker queryLocalInterface(String descriptor) 使用接口描述符查询远程对象。
boolean removeDeathRecipient(IRemoteObject.DeathRecipient recipient, int flags) 取消注册用于接收远程代理死亡通知的回调。
boolean sendRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) 向 peer 对象发送请求。
protected void slowPathDump(FileDescriptor fd, PrintWriter pw, String[] args) 使用给定的 PrintWriter 对象将数据异步转储到指定文件。
void slowPathDump(FileDescriptor fd, String[] args) 将数据异步转储到指定文件。
从类 java.lang.Object 继承的方法
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

构造函数详细信息

RemoteObject

public RemoteObject(String descriptor)

用于使用默认接口描述符创建 RemoteObject 实例的构造函数。

参数:

参数名称 参数描述
descriptor 指示用于创建 RemoteObject 实例的接口描述符。

方法详情

queryLocalInterface

public IRemoteBroker queryLocalInterface(String descriptor)

使用接口描述符查询远程对象。

指定者:

接口 IRemoteObject 中的 queryLocalInterface

参数:

参数名称 参数描述
descriptor 表示用于查询远程对象的接口描述符。

返回:

返回与接口描述符匹配的远程对象; 如果没有找到这样的远程对象,则返回 null。

addDeathRecipient

public boolean addDeathRecipient(IRemoteObject.DeathRecipient recipient, int flags)

注册一个用于接收远程代理死亡通知的回调。

指定者:

接口 IRemoteObject 中的 addDeathRecipient

参数:

参数名称 参数描述
recipient 表示要注册的回调。
flags 表示死亡通知的标志。

返回:

返回 false,因为不需要此回调。

removeDeathRecipient

public boolean removeDeathRecipient(IRemoteObject.DeathRecipient recipient, int flags)

取消注册用于接收远程代理死亡通知的回调。

指定者:

接口 IRemoteObject 中的 removeDeathRecipient

参数:

参数名称 参数描述
recipient 指示要注销的回调。
flags 表示死亡通知的标志。

返回:

返回 false,因为不需要此回调。

getInterfaceDescriptor

public String getInterfaceDescriptor()

查询接口描述符。

指定者:

接口 IRemoteObject 中的 getInterfaceDescriptor

返回:

返回接口描述符。

onRemoteRequest

public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) throws RemoteException

设置接收请求的条目。

该方法由远程服务提供者实现。 使用 IPC 时,您需要使用自己的服务逻辑覆盖此方法。

参数:

参数名称 参数描述
code 表示对端发送的服务请求码。
data 表示对端发送的MessageParcel 对象。
reply 表示远程服务发送的响应消息对象。 本地服务将响应数据写入 MessageParcel 对象。
option 指示操作是同步的还是异步的。

返回:

如果操作成功,则返回 true; 否则返回 false。

Throws:

Throw名称 Throw描述
RemoteException 如果发生远程服务错误,则抛出此异常。

sendRequest

public boolean sendRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) throws RemoteException

向 peer 对象发送请求。

如果对等对象和 RemoteObject 在同一设备上,则请求由 IPC 驱动程序发送。 如果它们在不同的设备上,则请求由套接字驱动程序发送。

指定者:

接口 IRemoteObject 中的 sendRequest

参数:

参数名称 参数描述
code 表示请求的消息代码。
data 表示存储待发送数据的MessageParcel 对象。
reply 指示接收响应数据的 MessageParcel 对象。
option 指示同步(默认)或异步请求。

返回:

如果操作成功,则返回 true; 否则返回 false。

Throws:

Throw名称 Throw描述
RemoteException 如果发生远程服务错误,则抛出此异常。

getCallingPid

public int getCallingPid()

获取 RemoteProxy 对象的 PID。

返回:

返回 RemoteProxy 对象的 PID。

getCallingUid

public int getCallingUid()

获取 RemoteProxy 对象的 UID。

返回:

返回 RemoteProxy 对象的 UID。

dump

protected void dump(FileDescriptor fd, PrintWriter pw, String[] args)

将当前接口的状态转储到具有特定描述符的文件中。

参数:

参数名称 参数描述
fd 表示目标文件的描述符。
pw 将对象的格式化表示打印到输出流。
args 以字符串形式表示该方法的参数。

Throws:

Throw名称 Throw描述
RemoteException 如果打开 FileOutputStream 或将数据写入 FileOutputStream 时发生错误,则抛出此异常。

slowPathDump

protected void slowPathDump(FileDescriptor fd, PrintWriter pw, String[] args)

使用给定的 PrintWriter 对象将数据异步转储到指定文件。

此方法仅适用于同一设备上的数据转储。

您需要重写此方法以定义要转储到文件的数据。 转储完成后,您需要关闭文件描述符。

参数:

参数名称 参数描述
fd 表示目标文件的文件描述符。
pw 指示用于将数据转储到目标文件的 PrintWriter 对象,禁用自动刷新。
args 指示要转储的参数(字符串数组)。

dump

public void dump(FileDescriptor fd, String[] args) throws RemoteException

将当前接口的状态转储到具有特定描述符的文件中。

指定者:

在接口 IRemoteObject 中转储

参数:

参数名称 参数描述
fd 表示目标文件的描述符。
args 以字符串形式表示该方法的参数。

Throws:

Throw名称 Throw描述
RemoteException 如果打开 FileOutputStream 或将数据写入 FileOutputStream 时发生错误,则抛出此异常。

slowPathDump

public void slowPathDump(FileDescriptor fd, String[] args) throws RemoteException

将数据异步转储到指定文件。

此方法仅适用于同一设备上的数据转储。

或者,您可以覆盖 [slowPathDump(java.io.FileDescriptor,java.io.PrintWriter,java.lang.String]) 方法来定义要转储到文件的数据。 转储完成后,您需要关闭文件描述符。

指定者:

接口 IRemoteObject 中的 slowPathDump

参数:

参数名称 参数描述
fd 表示目标文件的文件描述符。
args 指示要转储的参数(字符串数组)。

Throws:

Throw名称 Throw描述
RemoteException 如果转储命令发送失败或转储过程中发生错误,则抛出此异常。

finalize

protected void finalize() throws Throwable

从类复制的描述:对象

当垃圾收集确定不再有对该对象的引用时,由对象上的垃圾收集器调用。子类覆盖 finalize 方法以释放系统资源或执行其他清理。

finalize 的一般约定是,当 Java™ 虚拟机确定不再有任何方法可以让任何尚未终止的线程访问此对象时调用它,除非是由于某个操作由准备完成的其他对象或类的完成所采取。 finalize 方法可以采取任何行动,包括使该对象再次可供其他线程使用;然而,finalize 的通常目的是在对象被不可撤销地丢弃之前执行清理操作。例如,代表输入/输出连接的对象的 finalize 方法可能会执行显式 I/O 事务以在对象被永久丢弃之前中断连接。

Object 类的 finalize 方法不执行任何特殊操作;它只是正常返回。 Object 的子类可以覆盖这个定义。

Java 编程语言不保证哪个线程将为任何给定对象调用 finalize 方法。但是,可以保证调用 finalize 的线程在调用 finalize 时不会持有任何用户可见的同步锁。如果 finalize 方法抛出未捕获的异常,则忽略该异常并终止该对象的终结。

在为一个对象调用 finalize 方法之后,不会采取进一步的行动,直到 Java 虚拟机再次确定没有任何方法可以让任何尚未终止的线程访问该对象,包括可能的行动由其他准备完成的对象或类,此时该对象可能被丢弃。

对于任何给定的对象,Java 虚拟机永远不会多次调用 finalize 方法。

finalize 方法抛出的任何异常都会导致该对象的终结被暂停,否则会被忽略。

覆盖:

在类 Object 中完成

Throws:

Throw名称 Throw描述
Throwable 此方法引发的异常

isObjectDead

public boolean isObjectDead()

检查 RemoteObject 是否已失效。

指定者:

接口 IRemoteObject 中的 isObjectDead

返回:

默认返回 false。

attachLocalInterface

public void attachLocalInterface(IRemoteBroker localInterface, String descriptor)

修改当前 RemoteObject 的描述。

此方法用于更改在创建 RemoteObject 期间指定的默认描述符。

参数:

参数名称 参数描述
localInterface 指示要更改其描述符的 RemoteObject。
descriptor 指示 RemoteObject 的新描述符。
鸿蒙OS ReliableFileDescriptor.AutoCloseFileOutputStream
鸿蒙OS RemoteObjectCallbackList
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

鸿蒙OS 开发

鸿蒙OS 术语

鸿蒙OS Java API参考

鸿蒙OS ohos.aafwk.ability

鸿蒙OS ohos.aafwk.abilityjet.activedata

鸿蒙OS ohos.aafwk.content

鸿蒙OS java.lang

鸿蒙OS java.Util

鸿蒙OS java.Util class

鸿蒙OS ohos.data.dataability

鸿蒙OS ohos.data.dataability class

鸿蒙OS ohos.agp.components

鸿蒙OS ohos.agp.components interface

鸿蒙OS ohos.agp.components class

鸿蒙OS ohos.global.configuration

鸿蒙OS java.io

鸿蒙OS ohos.data.resultset

鸿蒙OS ohos.data.resultset interface

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }