public class DataAbilityHelper extends Object
DataAbilityHelper is a helper class for data operations. It uses IDataAbilityObserver to
register an observer for monitoring data. You can call creator(ohos.app.Context) to create a
DataAbilityHelper instance without specifying the Uri or call
creator(ohos.app.Context,ohos.utils.net.Uri,boolean) to create one with a given Uri.
IDataAbilityObserver,
Context,
UriSystemCapability.Aafwk#ABILITY| 限定符和类型 | 方法 | 说明 |
|---|---|---|
int |
batchInsert(Uri uri,
ValuesBucket[] values) |
Inserts multiple data records into the database.
|
PacMap |
call(Uri uri,
String method,
String arg,
PacMap extras) |
Calls the method of the Data ability.
|
static DataAbilityHelper |
create(Context context) |
|
static DataAbilityHelper |
create(Context context,
Uri uri) |
|
static DataAbilityHelper |
create(Context context,
Uri uri,
boolean tryBind) |
Creates a
DataAbilityHelper instance in the given Context. |
static DataAbilityHelper |
create(Context context,
Uri uri,
boolean tryBind,
int flag) |
Creates a
DataAbilityHelper instance. |
static DataAbilityHelper |
creator(Context context) |
已过时。
This change is effective since API version 7. Use
create(Context context) instead. |
static DataAbilityHelper |
creator(Context context,
Uri uri) |
已过时。
This change is effective since API version 7. Use
create(Context context, Uri uri) instead. |
static DataAbilityHelper |
creator(Context context,
Uri uri,
boolean tryBind) |
已过时。
This change is effective since API version 7.
Use
create(Context context, Uri uri, boolean tryBind) instead. |
int |
delete(Uri uri,
DataAbilityPredicates predicates) |
Deletes one or more data records from the database.
|
Uri |
denormalizeUri(Uri uri) |
Converts the given normalized
uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one. |
DataAbilityResult[] |
executeBatch(Uri uri,
ArrayList<DataAbilityOperation> operations) |
Performs batch operations on the database.
|
String[] |
getFileTypes(Uri uri,
String mimeTypeFilter) |
Obtains the MIME types of files supported.
|
String |
getType(Uri uri) |
Obtains the MIME type of the date specified by the given URI.
|
int |
insert(Uri uri,
ValuesBucket value) |
Inserts a single data record into the database.
|
Uri |
normalizeUri(Uri uri) |
Converts the given
uri that refers to the Data ability into a normalized Uri. |
void |
notifyChange(Uri uri) |
Notifies the registered observers of a change to the data resource specified by
Uri. |
InputStream |
obtainInputStream(Uri uri) |
Obtains the input stream of a file.
|
OutputStream |
obtainOutputStream(Uri uri) |
Obtains the output stream of a file.
|
FileDescriptor |
openFile(Uri uri,
String mode) |
Opens a file in a specified remote path.
|
RawFileDescriptor |
openRawFile(Uri uri,
String mode) |
This is like
openFile(ohos.utils.net.Uri, java.lang.String), open a file that need to be able to
return sub-sections of files,often assets inside of their .hap. |
ResultSet |
query(Uri uri,
String[] columns,
DataAbilityPredicates predicates) |
Queries data in the database.
|
void |
registerObserver(Uri uri,
IDataAbilityObserver dataObserver) |
Registers an observer to observe data specified by the given
Uri. |
boolean |
release() |
Releases the client resource of the Data ability.
|
boolean |
reload(Uri uri,
PacMap extras) |
Reloads data in the database.
|
void |
unregisterObserver(Uri uri,
IDataAbilityObserver dataObserver) |
Deregisters an observer used for monitoring data specified by the given
Uri. |
int |
update(Uri uri,
ValuesBucket value,
DataAbilityPredicates predicates) |
Updates data records in the database.
|
@Deprecated public static DataAbilityHelper creator(Context context)
create(Context context) instead.context - Indicates the Context object on the OHOS.DataAbilityHelper instance without a Uri.public static DataAbilityHelper create(Context context)
context - Indicates the Context object on the OHOS.DataAbilityHelper instance where Uri
is not specified.@Deprecated public static DataAbilityHelper creator(Context context, Uri uri)
create(Context context, Uri uri) instead.context - Indicates the Context object on the OHOS.uri - Indicates the database table or disk file to operate.DataAbilityHelper instance with the specified Uri.public static DataAbilityHelper create(Context context, Uri uri)
context - Indicates the Context object on the OHOS.uri - Indicates the database table or disk file to operate.DataAbilityHelper instance with the specified Uri.@Deprecated public static DataAbilityHelper creator(Context context, Uri uri, boolean tryBind)
create(Context context, Uri uri, boolean tryBind) instead.DataAbilityHelper instance in the given Context.
You can use this method to specify the Uri of the data to operate and set
the binding relationship between the ability of the Provider process and the associated client process
in a DataAbilityHelper instance.
context - Indicates the Context object on the OHOS.uri - Indicates the URI of the database table to operate.tryBind - Indicates the binding relationship between the Provider process and the associated client
process in a DataAbilityHelper instance. It determines whether the exit of the
corresponding Provider process causes the exit of the client process.DataAbilityHelper instance.public static DataAbilityHelper create(Context context, Uri uri, boolean tryBind)
DataAbilityHelper instance in the given Context.
You can use this method to specify the Uri of the data to operate and set
the binding relationship between the ability of the Provider process and the associated client process
in a DataAbilityHelper instance.
context - Indicates the Context object on the OHOS.uri - Indicates the URI of the database table to operate.tryBind - Indicates the binding relationship between the Provider process and the associated client
process in a DataAbilityHelper instance. It determines whether the exit of the
corresponding Provider process causes the exit of the client process.DataAbilityHelper instance.public static DataAbilityHelper create(Context context, Uri uri, boolean tryBind, int flag)
DataAbilityHelper instance.
You can use this method to specify the Uri of the data to operate, set
the binding relationship between the ability of the Provider process and the associated client process
in a DataAbilityHelper instance, and specify whether the passed URI is in the OHOS format.
context - Indicates the Context object on the OHOS.uri - Indicates the URI of the database table to operate.tryBind - Indicates the binding relationship between the Provider process and the associated client
process in a DataAbilityHelper instance. It determines whether the exit of the
corresponding Provider process causes the exit of the client process.flag - Indicates whether the passed URI is in the OHOS format. If the URI is not in the OHOS format,
pass Intent.FLAG_NOT_OHOS_COMPONENT.DataAbilityHelper instance.public boolean release()
You should call this method to releases client resource after the data operations are complete.
true if the resource is successfully released; returns false otherwise.public void registerObserver(Uri uri, IDataAbilityObserver dataObserver) throws IllegalArgumentException
Uri.uri - Indicates the path of the data to operate.dataObserver - Indicates the IDataAbilityObserver object.IllegalArgumentException - Throws this exception if a parameter value is invalid.NullPointerException - Throws this exception if uri is null.public void notifyChange(Uri uri) throws IllegalArgumentException
Uri.uri - Indicates the path of the data to operate.IllegalArgumentException - Throws this exception if a parameter value is invalid.NullPointerException - Throws this exception if uri is null.public void unregisterObserver(Uri uri, IDataAbilityObserver dataObserver) throws IllegalArgumentException
Uri.uri - Indicates the path of the data to operate.dataObserver - Indicates the IDataAbilityObserver object.IllegalArgumentException - Throws this exception if a parameter value is invalid.NullPointerException - Throws this exception if uri is null.public int insert(Uri uri, ValuesBucket value) throws DataAbilityRemoteException
uri - Indicates the path of the data to operate.value - Indicates the data record to insert. If this parameter is null, a blank row will be inserted.DataAbilityRemoteException - Throws this exception if a remote process exception occurs.IllegalStateException - Throws this exception if the dataAbility does not exist.NullPointerException - Throws this exception if uri is null.public int batchInsert(Uri uri, ValuesBucket[] values) throws DataAbilityRemoteException
uri - Indicates the path of the data to operate.values - Indicates the data records to insert.DataAbilityRemoteException - Throws this exception if a remote process exception occurs.IllegalStateException - Throws this exception if the dataAbility does not exist.IllegalArgumentException - Throws this exception if a parameter value is invalid.NullPointerException - Throws this exception if uri is null.public int delete(Uri uri, DataAbilityPredicates predicates) throws DataAbilityRemoteException
uri - Indicates the path of the data to operate.predicates - Indicates filter criteria. You should define the processing logic when this parameter is null.DataAbilityRemoteException - Throws this exception if a remote process exception occurs.IllegalStateException - Throws this exception if the dataAbility does not exist.NullPointerException - Throws this exception if uri is null.public int update(Uri uri, ValuesBucket value, DataAbilityPredicates predicates) throws DataAbilityRemoteException
uri - Indicates the path of data to update.value - Indicates the data to update. This parameter can be null.predicates - Indicates filter criteria. You should define the processing logic when this parameter is null.DataAbilityRemoteException - Throws this exception if a remote process exception occurs.IllegalStateException - Throws this exception if the dataAbility does not exist.NullPointerException - Throws this exception if uri is null.public ResultSet query(Uri uri, String[] columns, DataAbilityPredicates predicates) throws DataAbilityRemoteException
uri - Indicates the path of data to query.columns - Indicates the columns to query. If this parameter is null, all columns are queried.predicates - Indicates filter criteria. You should define the processing logic when this parameter is null.DataAbilityRemoteException - Throws this exception if a remote process exception occurs.IllegalStateException - Throws this exception if the dataAbility does not exist.NullPointerException - Throws this exception if uri is null.public DataAbilityResult[] executeBatch(Uri uri, ArrayList<DataAbilityOperation> operations) throws DataAbilityRemoteException, OperationExecuteException
uri - Indicates the path of data to operate.operations - Indicates a list of database operations on the database.DataAbilityRemoteException - Throws this exception if a remote process exception occurs.OperationExecuteException - Throws this exception if an operation exception occurs.IllegalStateException - Throws this exception if the dataAbility does not exist.IllegalArgumentException - Throws this exception if a parameter value is invalid.NullPointerException - Throws this exception if uri is null.public FileDescriptor openFile(Uri uri, String mode) throws DataAbilityRemoteException, FileNotFoundException
uri - Indicates the path of the file to open.mode - Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
(erasing whatever data is currently in the file), "wt" for write access that truncates any existing
file, "wa" for write-only access to append to any existing data, "rw" for read and write access on
any existing data, or "rwt" for read and write access that truncates any existing file.FileNotFoundException - Throws this exception if the file is not found.DataAbilityRemoteException - Throws this exception if a remote process exception occurs.IllegalStateException - Throws this exception if the dataAbility does not exist.NullPointerException - Throws this exception if uri or mode is null.public RawFileDescriptor openRawFile(Uri uri, String mode) throws DataAbilityRemoteException, FileNotFoundException
openFile(ohos.utils.net.Uri, java.lang.String), open a file that need to be able to
return sub-sections of files,often assets inside of their .hap.uri - Indicates the path of the file to open.mode - Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
(erasing whatever data is currently in the file), "wt" for write access that truncates any existing
file, "wa" for write-only access to append to any existing data, "rw" for read and write access on
any existing data, or "rwt" for read and write access that truncates any existing file.RawFileDescriptor object containing file descriptor.FileNotFoundException - Throws this exception if the file is not found.DataAbilityRemoteException - Throws this exception if a remote process exception occurs.IllegalStateException - Throws this exception if the dataAbility does not exist.NullPointerException - Throws this exception if uri or mode is null.public String[] getFileTypes(Uri uri, String mimeTypeFilter) throws DataAbilityRemoteException
uri - Indicates the path of the files to obtain.mimeTypeFilter - Indicates the MIME types of the files to obtain. This parameter cannot be null.
1. "*/*": Obtains all types supported by Data abilities.
2. "image/*": Obtains files whose main type is image of any subtype.
3. "*/jpg": Obtains files whose subtype is JPG of any main type.
null is returned.DataAbilityRemoteException - Throws this exception if a remote process exception occurs.NullPointerException - Throws this exception if uri or mimeTypeFilter is null.public PacMap call(Uri uri, String method, String arg, PacMap extras) throws DataAbilityRemoteException
uri - Indicates the Data ability of the method to call.method - Indicates the method to call.arg - Indicates the parameter of the String type.extras - Indicates the parameter of the PacMap type. If a custom Sequenceable
object is put in the PacMap object and will be transferred across processes, you
must call BasePacMap.setClassLoader(ClassLoader) to set a class loader for
the custom object. If the PacMap object is to be transferred to a non-OHOS process,
values of primitive types are supported, but not custom Sequenceable objects.DataAbilityRemoteException - Throws this exception if a remote process exception occurs.NullPointerException - Throws this exception if uri or method is null.public String getType(Uri uri) throws DataAbilityRemoteException
uri - Indicates the path of the data to operate.uri.DataAbilityRemoteException - Throws this exception if a remote process exception occurs.NullPointerException - Throws this exception if uri is null.public Uri normalizeUri(Uri uri) throws DataAbilityRemoteException
uri that refers to the Data ability into a normalized Uri.
A normalized URI can be used across devices, persisted, backed up, and restored.
To transfer a normalized URI from another environment to the current environment, you should call this
method again to re-normalize the URI for the current environment or call denormalizeUri(ohos.utils.net.Uri)
to convert it to a denormalized URI that can be used only in the current environment.
uri - Indicates the Uri object to normalize.Uri object if the Data ability supports URI normalization;
returns null otherwise.DataAbilityRemoteException - Throws this exception if the remote process exits.NullPointerException - Throws this exception if uri is null.denormalizeUri(ohos.utils.net.Uri)public Uri denormalizeUri(Uri uri) throws DataAbilityRemoteException
uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one.uri - Indicates the Uri object to denormalize.Uri object if the denormalization is successful; returns the
original Uri passed to this method if there is nothing to do; returns null if the data
identified by the normalized Uri cannot be found in the current environment.DataAbilityRemoteException - Throws this exception if the remote process exits.NullPointerException - Throws this exception if uri is null.normalizeUri(ohos.utils.net.Uri)public boolean reload(Uri uri, PacMap extras) throws DataAbilityRemoteException
uri - Indicates the position where the data is to reload. This parameter is mandatory.extras - Indicates the PacMap object containing the additional parameters to be passed
in this call. This parameter can be null. If a custom Sequenceable object is put in
the PacMap object and will be transferred across processes, you must call
BasePacMap.setClassLoader(ClassLoader) to set a class loader for the custom object.true if the data is successfully reloaded; returns false otherwise.DataAbilityRemoteException - Throws this exception if the remote process is abnormal.public InputStream obtainInputStream(Uri uri) throws DataAbilityRemoteException, FileNotFoundException
Ability.openFile(Uri, String) method before using this method.uri - Indicates the path of the file to open.FileNotFoundException - Throws this exception if the file is not found.DataAbilityRemoteException - Throws this exception if a remote process exception occurs.IllegalArgumentException - Throws this exception if a parameter value is invalid.IllegalStateException - Throws this exception if the specified URI does not exist.NullPointerException - Throws this exception if uri is null.public OutputStream obtainOutputStream(Uri uri) throws DataAbilityRemoteException, FileNotFoundException
Ability.openFile(Uri, String) method before using this method.uri - Indicates the path of the file to open.FileNotFoundException - Throws this exception if the file is not found.DataAbilityRemoteException - Throws this exception if a remote process exception occurs.IllegalArgumentException - Throws this exception if a parameter value is invalid.IllegalStateException - Throws this exception if the specified URI does not exist.NullPointerException - Throws this exception if uri is null.