public class DeviceManager extends Object
Intent, and initializing the distributed execution framework for a device.
If your application needs to be notified when a device on the distributed network becomes online or offline,
call DeviceManager#registerDeviceStateCallback(IDeviceStateCallback) to register device state callbacks.
If such notifications are no longer required, call
DeviceManager#unregisterDeviceStateCallback(IDeviceStateCallback) to unregister these callbacks.
To obtain information about all remote devices on the distributed network,
call DeviceManager#getDeviceList(int). To obtain information about a specified remote device,
call DeviceManager#getDeviceInfo(String).
To query information about a non-HarmonyOS remote application on the distributed network,
call DeviceManager#queryRemoteAbilityByIntent(Intent).
If your application needs to invoke a component of a non-HarmonyOS application installed on a remote device,
call DeviceManager#initDistributedEnvironment(String, IInitCallback) to initialize
the distributed execution framework. In this way, your application will be notified of the initialization result
through the specified callback. After the cross-device task is complete,
call DeviceManager#unInitDistributedEnvironment(String, IInitCallback) to deactivate
the distributed execution framework and unregister the previously registered callbacks.
IDeviceStateCallback,
IInitCallback| 限定符和类型 | 方法 | 说明 |
|---|---|---|
static DeviceInfo |
getDeviceInfo(String deviceId) |
Obtains information about a specified remote device on the distributed network
based on the given network ID.
|
static List<DeviceInfo> |
getDeviceList(int flag) |
Obtains information about all remote devices on the distributed network.
|
static void |
initDistributedEnvironment(String deviceId,
IInitCallback callback) |
Initializes the distributed execution framework on the specified device and registers callbacks for
notifying the initialization result.
|
static List<AbilityInfo> |
queryRemoteAbilityByIntent(Intent intent) |
Queries information about a particular remote application on the current distributed network
based on the specified
Intent. |
static boolean |
registerDeviceStateCallback(IDeviceStateCallback callback) |
Registers a custom device state callback so that the application can
be notified upon state changes of devices on the distributed network.
|
static void |
unInitDistributedEnvironment(String deviceId,
IInitCallback callback) |
Deactivates the distributed execution framework on the specified device and unregisters the callback previously
registered by calling
initDistributedEnvironment(java.lang.String,ohos.distributedschedule.interwork.IInitCallback). |
static boolean |
unregisterDeviceStateCallback(IDeviceStateCallback callback) |
Unregisters all custom device state callbacks previously registered
through
registerDeviceStateCallback(ohos.distributedschedule.interwork.IDeviceStateCallback). |
public static boolean registerDeviceStateCallback(IDeviceStateCallback callback)
To register a callback for device state changes,
you should first implement the IDeviceStateCallback interface and
override the IDeviceStateCallback#onDeviceOnline(String, int) and
IDeviceStateCallback#onDeviceOffline(String, int) methods.
The two methods will be called upon device state changes.
Do not repeatedly register the same device state callback for an application. Otherwise, the registration fails.
Each process can register a maximum of 10 device state callbacks.
To use this method, your application must have gained the
ohos.permission.DISTRIBUTED_DEVICE_STATE_CHANGE permission.
callback - Indicates the callback object to register.
You must first implement the IDeviceStateCallback interface.true if the registration is successful; returns false otherwise.SecurityException - Throws this exception if your application does not have the required permissions.public static boolean unregisterDeviceStateCallback(IDeviceStateCallback callback)
registerDeviceStateCallback(ohos.distributedschedule.interwork.IDeviceStateCallback).
Generally, you first call registerDeviceStateCallback(ohos.distributedschedule.interwork.IDeviceStateCallback) to
register device state callbacks and then call this method
if your application no longer needs to receive any state change notifications.
If this method is called on callbacks that are not registered, the unregistration fails.
To use this method, your application must have gained the
ohos.permission.DISTRIBUTED_DEVICE_STATE_CHANGE permission.
callback - Indicates the callback object to unregister.
You must first implement the IDeviceStateCallback interface.true if the unregistration is successful; returns false otherwise.SecurityException - Throws this exception if your application does not have the required permissions.public static List<DeviceInfo> getDeviceList(int flag)
To use this method, your application must have gained the
ohos.permission.GET_DISTRIBUTED_DEVICE_INFO permission.
flag - Indicates the flag used for querying specified devices.
The value DeviceInfo#FLAG_GET_ALL_DEVICE means to query all online and
offline devices on the distributed network; DeviceInfo#FLAG_GET_ONLINE_DEVICE
means to query all online devices on the distributed network;
and DeviceInfo#FLAG_GET_OFFLINE_DEVICE means to query all offline devices on the distributed network.SecurityException - Throws this exception if your application does not have the required permissions.public static DeviceInfo getDeviceInfo(String deviceId)
To use this method, your application must have gained the
ohos.permission.GET_DISTRIBUTED_DEVICE_INFO permission.
deviceId - Indicates the network ID of the device.null if the query fails or the device is not found.SecurityException - Throws this exception if your application does not have the required permissions.public static List<AbilityInfo> queryRemoteAbilityByIntent(Intent intent)
Intent.
You can use this method to query only non-HarmonyOS applications installed on remote devices.
To use this method, your application must have gained
the ohos.permission.GET_BUNDLE_INFO permission.
intent - Indicates the Intent containing
the bundle information and device network ID of the application to query.AbilityInfo objects for the desired application.SecurityException - Throws this exception if your application does not have the required permission.public static void initDistributedEnvironment(String deviceId, IInitCallback callback) throws RemoteException
To enable your application to invoke a component of a non-HarmonyOS application installed on a
remote device on the distributed network, you should first call this method to initialize the distributed
execution framework on the specified device. If the initialization is successful, your application will be
notified through IInitCallback#onInitSuccess(String). If the initialization fails, your application will
be notified through IInitCallback#onInitFailure(String, int).
To use this method, your application must have gained
the ohos.permission.DISTRIBUTED_DATASYNC permission.
deviceId - Indicates the network ID of the device on which the distributed execution framework will be
initialized.callback - Indicates the callback to be invoked upon a successful or failed initialization attempt.IllegalArgumentException - Throws this exception if argument is null.RemoteException - Throws this exception if the IPC communication exception happens.public static void unInitDistributedEnvironment(String deviceId, IInitCallback callback) throws RemoteException
initDistributedEnvironment(java.lang.String,ohos.distributedschedule.interwork.IInitCallback).
After this method is called, the application notifies the distributed execution framework that the cross-device is complete and then unregisters the initialization result callback.
Your application should call this method after the cross-device task is complete.
To use this method, your application must have gained
the ohos.permission.DISTRIBUTED_DATASYNC permission.
deviceId - Indicates the network ID of the device on which the distributed execution framework will be
deactivated.callback - Indicates the callback to unregister.IllegalArgumentException - Throws this exception if argument is null.RemoteException - Throws this exception if the IPC communication exception happens.