ContextAbility, AbilityPackage, AbilitySlice, Fractionpublic abstract class AbilityContext extends Object implements Context
Context interface.
Applications can use AbilityContext to obtain resources, start abilities, create or get thread models,
and obtain the application package information and running information. Ability is
a child class of AbilityContext. Therefore, an ability is an instance of AbilityContext.
CONTEXT_IGNORE_SECURITY, CONTEXT_INCLUDE_CODE, CONTEXT_RESOUCE_ONLY, CONTEXT_RESTRICTED, MODE_APPEND, MODE_PRIVATE| 构造器 | 说明 |
|---|---|
AbilityContext() |
Default constructor of
AbilityContext. |
AbilityContext(Context context) |
A constructor used to create and initialize an ability.
|
| 限定符和类型 | 方法 | 说明 |
|---|---|---|
void |
attachBaseContext(Context base) |
Attaches a
Context object to the current ability. |
boolean |
canRequestPermission(String permission) |
Confirms with the permission management module to check whether a request prompt is required for granting a
certain permission.
|
void |
compelVerifyCallerPermission(String permission,
String message) |
Checks whether a specified permission has been granted to the calling process for inter-process
communication (IPC), and throws a
SecurityException if the permission is not granted. |
void |
compelVerifyCallerUriPermission(Uri uri,
int modeFlags,
String message) |
Checks whether the calling process for IPC has the permission to access a specified URI, and throws
a
SecurityException if it does not have the permission. |
void |
compelVerifyPermission(String permission,
int pid,
int uid,
String message) |
Checks whether a specified permission has been granted to the process identified by
pid and uid,
and throws a SecurityException if the permission is not granted. |
void |
compelVerifyPermission(String permission,
String message) |
Checks whether a specified permission has been granted to the calling process, and throws
a
SecurityException if the permission is not granted. |
void |
compelVerifyUriPermission(Uri uri,
int pid,
int uid,
int modeFlags,
String message) |
Checks whether the process identified by
pid and uid has the permission to access
a specified URI, and throws a SecurityException if it does not have the permission. |
void |
compelVerifyUriPermission(Uri uri,
int modeFlags,
String message) |
Checks whether the calling process has the permission to access a specified URI, and throws
a
SecurityException if it does not have the permission. |
void |
compelVerifyUriPermission(Uri uri,
String readPermission,
String writePermission,
int pid,
int uid,
int modeFlags,
String message) |
Checks whether the process identified by
pid and uid has specified read and write permissions
as well as the permission to access a specified URI, and throws a SecurityException if it does not have
the permissions. |
boolean |
connectAbility(Intent intent,
IAbilityConnection conn) |
Connects the current ability to an ability using the
AbilityInfo.AbilityType.SERVICE
template. |
Context |
createBundleContext(String bundleName,
int flag) |
Creates a
Context object for an application with the given bundle name. |
TaskDispatcher |
createParallelTaskDispatcher(String name,
TaskPriority priority) |
Creates a parallel task dispatcher with a specified priority.
|
TaskDispatcher |
createSerialTaskDispatcher(String name,
TaskPriority priority) |
Creates a serial task dispatcher with a specified priority.
|
boolean |
deleteFile(String fileName) |
Deletes the specified private file associated with the application.
|
void |
disconnectAbility(IAbilityConnection conn) |
Disconnects the current ability from an ability using the
AbilityInfo.AbilityType.SERVICE template. |
void |
displayUnlockMissionMessage() |
Displays a system-defined message to the user, prompting the user how to exit the lock mission mode.
|
AbilityInfo |
getAbilityInfo() |
Obtains information about the current ability.
|
IAbilityManager |
getAbilityManager() |
Obtains an ability manager.
|
Context |
getAbilityPackageContext() |
Obtains the
Context object of this ability. |
Context |
getApplicationContext() |
Obtains the
Context object of the application. |
ApplicationInfo |
getApplicationInfo() |
Obtains information about the current application.
|
String |
getAppType() |
Obtains the type of this application.
|
String |
getBundleCodePath() |
Obtains the path of the package containing the current ability.
|
IBundleManager |
getBundleManager() |
Obtains an
IBundleManager instance. |
String |
getBundleName() |
Obtains the bundle name of the current ability.
|
String |
getBundleResourcePath() |
Obtains the path of the Ohos Ability Package (HAP} containing this ability.
|
File |
getCacheDir() |
Obtains the application-specific cache directory on the device's internal storage.
|
Uri |
getCaller() |
Obtains information about the caller of this ability.
|
ElementName |
getCallingAbility() |
Obtains the
ElementName of the ability that called the current ability. |
String |
getCallingBundle() |
Obtains the bundle name of the ability that called the current ability.
|
ClassLoader |
getClassloader() |
Obtains a
ClassLoader instance. |
File |
getCodeCacheDir() |
Obtains the application-specific code-cache directory on the device's internal storage.
|
int |
getColor(int resId) |
Get the color of this
Context based on the specified resource ID. |
int |
getColorMode() |
Obtains color mode.
|
Context |
getContext() |
Obtains the application context.
|
File |
getDatabaseDir() |
Obtains the local database path.
|
File |
getDataDir() |
Obtains the absolute path where all private data files of this application are stored.
|
File |
getDir(String name,
int mode) |
Obtains the directory for storing custom data files of the application.
|
int |
getDisplayOrientation() |
Obtains the current display orientation of this ability.
|
File |
getDistributedDir() |
Obtains the distributed file path.
|
ElementName |
getElementName() |
Obtains the
ohos.bundle.ElementName object of the current ability. |
File |
getExternalCacheDir() |
Obtains the absolute path to the application-specific cache directory on the primary external or shared
storage device.
|
File[] |
getExternalCacheDirs() |
Obtains the absolute paths to the application-specific cache directories on all external or shared storage
devices.
|
File |
getExternalFilesDir(String type) |
Obtains the absolute path to the directory for storing files for the application on the primary external or
shared storage device.
|
File[] |
getExternalFilesDirs(String type) |
Obtains the absolute paths to the directories for storing files for the application on all external or shared
storage devices.
|
File[] |
getExternalMediaDirs() |
Obtains the application-specific media directories on all files where it can place.
|
File |
getFilesDir() |
Obtains the directory for storing files for the application on the device's internal storage.
|
TaskDispatcher |
getGlobalTaskDispatcher(TaskPriority priority) |
Obtains a global task dispatcher with a specified priority.
|
HapModuleInfo |
getHapModuleInfo() |
Obtains the
HapModuleInfo object of the application. |
int[] |
getIntArray(int resId) |
Get the integer array of this
Context based on the specified resource ID. |
Object |
getLastStoredDataWhenConfigChanged() |
Obtains the most recent data saved via
Ability.onStoreDataWhenConfigChange()
when the system configuration is changed. |
String |
getLocalClassName() |
Obtains the local class name of the ability.
|
TaskDispatcher |
getMainTaskDispatcher() |
Obtains a task dispatcher that is bound to the application main thread.
|
File |
getNoBackupFilesDir() |
Obtains the absolute path which app created and will be excluded from automatic backup to remote storage.
|
Pattern |
getPattern() |
Obtains the pattern of this
Context. |
File |
getPreferencesDir() |
Obtains the path storing the preference file of the application.
|
ProcessInfo |
getProcessInfo() |
Obtains information about the current process, including the process ID and name.
|
String |
getProcessName() |
Obtains the name of the current process.
|
ResourceManager |
getResourceManager() |
Obtains a resource manager.
|
ResourceManager |
getResourceManager(Configuration configuration) |
Obtains a resource manager based on the specified
Configuration object. |
String |
getString(int resId) |
Get the string of this
Context based on the specified resource ID. |
String |
getString(int resId,
Object... formatArgs) |
Get the string of this
Context based on the specified resource ID and formatArgs. |
String[] |
getStringArray(int resId) |
Get the string array of this
Context based on the specified resource ID. |
Theme |
getTheme() |
Obtains the theme of this
Context. |
int |
getThemeId() |
Obtains the theme id of this
Context. |
TaskDispatcher |
getUITaskDispatcher() |
Obtains a task dispatcher that is bound to the UI thread.
|
void |
init() |
Initializes the current ability.
|
boolean |
isCredentialEncryptedStorage() |
Checks whether the context uses credential-encrypted storage.
|
boolean |
isDeviceEncryptedStorage() |
Checks whether the context uses device-protected storage.
|
boolean |
isUpdatingConfigurations() |
Checks whether the configuration of this ability is changing
|
void |
lockMission() |
Sets the application to start its ability in lock mission mode.
|
void |
printDrawnCompleted() |
Prints out the time required for the system to complete drawing this Page ability.
|
void |
requestPermissionsFromUser(String[] permissions,
int requestCode) |
Requests certain permissions from the system.
|
void |
restart() |
Restarts this ability using a new instance.
|
void |
setColorMode(int mode) |
Set color mode.
|
void |
setDisplayOrientation(AbilityInfo.DisplayOrientation newOrientation) |
Sets the display orientation of the current ability.
|
void |
setPattern(int patternId) |
Sets the pattern of this
Context based on the specified pattern ID. |
void |
setShowOnLockScreen(boolean showOnLockScreen) |
Sets whether to show this ability on top of the lock screen whenever the lock screen is displayed,
keeping the ability in the ACTIVE state.
|
void |
setTheme(int themeId) |
Sets the theme of this
Context based on the specified theme ID. |
void |
setTransitionAnimation(int enterAnim,
int exitAnim) |
Sets the transition animation between two abilities.
|
void |
setWakeUpScreen(boolean wakeUpScreen) |
Sets whether to wake up the screen when this ability is restored.
|
void |
startAbilities(Intent[] intents) |
Starts multiple abilities.
|
void |
startAbility(Intent intent,
int requestCode) |
Starts a new ability.
|
void |
startAbility(Intent intent,
int requestCode,
AbilityStartSetting abilityStartSetting) |
Starts a new ability with special ability start setting.
|
void |
startAbility(Intent intent,
AbilityStartSetting abilityStartSetting,
IAbilityStartCallback callback) |
Starts a new ability.
|
boolean |
stopAbility(Intent intent) |
Destroys another ability that uses the
AbilityInfo.AbilityType.SERVICE template. |
void |
switchToCredentialEncryptedStorageContext() |
Changes the context storage to credential-protected storage.
|
void |
switchToDeviceEncryptedStorageContext() |
Changes the context storage to device-protected storage.
|
void |
terminateAbility() |
Destroys the current ability.
|
void |
terminateAbility(int requestCode) |
Destroys another ability you had previously started by calling
Ability.startAbilityForResult(ohos.aafwk.content.Intent, int, ohos.aafwk.ability.startsetting.AbilityStartSetting) with the same requestCode passed. |
boolean |
terminateAbilityResult(int startId) |
Destroys this Service ability if the number of times it has been started equals the number represented by the
given
startId. |
void |
unlockMission() |
Unlocks this ability by exiting the lock mission mode.
|
int |
verifyCallingOrSelfPermission(String permission) |
Checks whether the calling or current process has the given permission.
|
int |
verifyCallingPermission(String permission) |
Checks whether the calling process for inter-process communication has the given permission.
|
int |
verifyPermission(String permission,
int pid,
int uid) |
Checks whether a process ID (PID) and user ID (UID) have the given permission.
|
int |
verifySelfPermission(String permission) |
Checks whether the current process has the given permission.
|
public AbilityContext()
AbilityContext.public AbilityContext(Context context)
context - Indicates the application context. This parameter cannot be null.public void attachBaseContext(Context base)
Context object to the current ability.
Generally, this method is called after Ability is loaded to provide the
application context for the current ability.
base - Indicates a Context object.public final Context getContext()
attachBaseContext(ohos.app.Context).public void init()
public final TaskDispatcher getUITaskDispatcher()
ContextTasks dispatched on this dispatcher are executed in sequence. Do not use this dispatcher on the UI thread for running synchronous tasks, Otherwise, a deadlock occurs.
getUITaskDispatcher 在接口中 ContextContext.getUITaskDispatcher()public final TaskDispatcher getMainTaskDispatcher()
ContextTasks dispatched on this dispatcher are executed in sequence. Do not use this dispatcher on the main thread for running synchronous tasks, Otherwise, a deadlock occurs.
getMainTaskDispatcher 在接口中 ContextContext.getMainTaskDispatcher()public TaskDispatcher createParallelTaskDispatcher(String name, TaskPriority priority)
ContextUnlike a global task dispatcher created by using Context.getGlobalTaskDispatcher(ohos.app.dispatcher.task.TaskPriority), a parallel
task dispatcher can use separate task groups to run tasks in a group in parallel and run different task groups
in sequence.
createParallelTaskDispatcher 在接口中 Contextname - Indicates the task dispatcher name. This parameter is used to locate problems.priority - Indicates the priority of all tasks dispatched by the parallel task dispatcher.Context.createParallelTaskDispatcher(String, TaskPriority)public TaskDispatcher createSerialTaskDispatcher(String name, TaskPriority priority)
ContextIf you want to execute tasks in a specific sequence, use a serial task dispatcher. Otherwise, use a parallel task dispatcher.
createSerialTaskDispatcher 在接口中 Contextname - Indicates the task dispatcher name. This parameter is used to locate problems.priority - Indicates the priority of all tasks dispatched by the created task dispatcher.Context.createSerialTaskDispatcher(String, TaskPriority)public TaskDispatcher getGlobalTaskDispatcher(TaskPriority priority)
ContextYou can use a global task dispatcher to dispatch tasks that are independent from each other. Do not create task groups or execution barriers on this dispatcher.
getGlobalTaskDispatcher 在接口中 Contextpriority - Indicates the priority of all tasks dispatched by the global task dispatcher.Context.getGlobalTaskDispatcher(TaskPriority)public ApplicationInfo getApplicationInfo()
ContextThe returned application information includes basic information such as the application name and application permissions.
getApplicationInfo 在接口中 ContextApplicationInfo object for the current application.Context.getApplicationInfo()public AbilityInfo getAbilityInfo()
ContextThe returned information includes the class name, bundle name, and other information about the current ability.
getAbilityInfo 在接口中 ContextAbilityInfo object for the current ability.Context.getAbilityInfo()public ProcessInfo getProcessInfo()
ContextgetProcessInfo 在接口中 ContextProcessInfo object for the current process.Context.getProcessInfo()public ResourceManager getResourceManager()
ContextgetResourceManager 在接口中 ContextResourceManager object.Context.getResourceManager()public File getPreferencesDir()
ContextIf the preference file path does not exist, the system creates one and returns the created path.
getPreferencesDir 在接口中 ContextContext.getPreferencesDir()public File getDatabaseDir()
ContextIf the local database path does not exist, the system creates one and returns the created path.
getDatabaseDir 在接口中 ContextContext.getDatabaseDir()public File getDistributedDir()
ContextIf the distributed file path does not exist, the system creates one and returns the created path. This method is applicable only to the context of an ability rather than that of an application.
getDistributedDir 在接口中 ContextContext.getDistributedDir()public void switchToDeviceEncryptedStorageContext()
ContextswitchToDeviceEncryptedStorageContext 在接口中 ContextContext.switchToDeviceEncryptedStorageContext()public void switchToCredentialEncryptedStorageContext()
ContextswitchToCredentialEncryptedStorageContext 在接口中 ContextContext.switchToCredentialEncryptedStorageContext()public boolean isDeviceEncryptedStorage()
ContextisDeviceEncryptedStorage 在接口中 Contexttrue if the context uses device-protected storage; returns false otherwise.Context.isDeviceEncryptedStorage()public boolean isCredentialEncryptedStorage()
ContextisCredentialEncryptedStorage 在接口中 Contexttrue if the context uses credential-encrypted storage; returns false otherwise.Context.isCredentialEncryptedStorage()public int verifyCallingPermission(String permission)
ContextThe calling process is not the current process.
verifyCallingPermission 在接口中 Contextpermission - Indicates the permission to check. This parameter cannot be null.0 (IBundleManager.PERMISSION_GRANTED) if the
calling process has the permission;
returns -1 (IBundleManager.PERMISSION_DENIED) otherwise.Context.verifyCallingPermission(String)public int verifySelfPermission(String permission)
ContextYou need to call Context.requestPermissionsFromUser(java.lang.String[],int) to request a permission only if the
current process does not have the specific permission.
verifySelfPermission 在接口中 Contextpermission - Indicates the permission to check. This parameter cannot be null.0 (IBundleManager.PERMISSION_GRANTED) if the
current process has the permission; returns -1
(IBundleManager.PERMISSION_DENIED) otherwise.Context.verifySelfPermission(String)public int verifyCallingOrSelfPermission(String permission)
ContextThis method checks permission for a calling process only if the call is from a remote device. Otherwise, it checks permission for the current process.
verifyCallingOrSelfPermission 在接口中 Contextpermission - Indicates the permission to check. This parameter cannot be null.0 (IBundleManager.PERMISSION_GRANTED) if the
calling or current process has the permission; returns -1
(IBundleManager.PERMISSION_DENIED) otherwise.Context.verifyCallingOrSelfPermission(String)public int verifyPermission(String permission, int pid, int uid)
ContextverifyPermission 在接口中 Contextpermission - Indicates the permission to check. This parameter cannot be null.pid - Indicates the process ID. The value must be greater than 0.uid - Indicates the user ID.0 (IBundleManager.PERMISSION_GRANTED) if the PID
and UID have the permission; returns -1
(IBundleManager.PERMISSION_DENIED) otherwise.Context.verifyPermission(String, int, int)public ClassLoader getClassloader()
ContextClassLoader instance.
A ClassLoader instance is used to load a class, for example, to load the class of the current
ability using the class name.
getClassloader 在接口中 ContextClassLoader instance.Context.getClassloader()public Context getApplicationContext()
ContextContext object of the application.getApplicationContext 在接口中 ContextContext object of the application.Context.getApplicationContext()public void terminateAbility()
ContextAfter the current ability using either the AbilityInfo.AbilityType.SERVICE or
AbilityInfo.AbilityType.PAGE template performs all operations, it can use this method to
destroy itself to free up memory.
terminateAbility 在接口中 ContextContext.terminateAbility()public void terminateAbility(int requestCode)
ContextAbility.startAbilityForResult(ohos.aafwk.content.Intent, int, ohos.aafwk.ability.startsetting.AbilityStartSetting) with the same requestCode passed.terminateAbility 在接口中 ContextrequestCode - Indicates the request code passed for starting the ability.Context.terminateAbility(int)public void displayUnlockMissionMessage()
ContextThis method takes effect only when the mission that contains this ability is in the
ohos.app.AbilityManager#LOCK_MISSION_MODE_LOCKED or
ohos.app.AbilityManager#LOCK_MISSION_MODE_PINNED state.
displayUnlockMissionMessage 在接口中 ContextContext.displayUnlockMissionMessage()public void lockMission()
ContextThis method takes effect only when the ability runs on the foreground.
lockMission 在接口中 ContextContext.lockMission()public void unlockMission()
ContextThis method takes effect only for abilities that have been put into the lock mission mode by the
Context.lockMission() method.
unlockMission 在接口中 ContextContext.unlockMission()public final boolean terminateAbilityResult(int startId)
ContextstartId. This method is the same as calling Context.terminateAbility() to destroy this Service
ability, except that this method helps you avoid destroying it if a client has requested a Service
ability startup in Ability.onCommand(ohos.aafwk.content.Intent, boolean) but you are unaware of it.terminateAbilityResult 在接口中 ContextstartId - Indicates the number of startup times of this Service ability passed to
Ability.onCommand(ohos.aafwk.content.Intent, boolean). The startId is
incremented by 1 every time this ability is started. For example,
if this ability has been started for six times, the value of startId is 6.true if the startId matches the number of startup times
and this Service ability will be destroyed; returns false otherwise.Context.terminateAbilityResult(int)public String getLocalClassName()
ContextgetLocalClassName 在接口中 ContextContext.getLocalClassName()public ElementName getElementName()
Contextohos.bundle.ElementName object of the current ability.
This method is available only to Page abilities.getElementName 在接口中 Contextohos.bundle.ElementName object of the current ability.Context.getElementName()public ElementName getCallingAbility()
ContextElementName of the ability that called the current ability.
You can use the obtained ElementName to check whether the calling ability is allowed
to receive the data you will send.
If you did not use Ability.startAbilityForResult(ohos.aafwk.content.Intent, int, ohos.aafwk.ability.startsetting.AbilityStartSetting) to start the calling
ability, null is returned.
getCallingAbility 在接口中 ContextElementName of the calling ability;
returns null if no calling ability is available.Context.getCallingAbility()public String getCallingBundle()
ContextYou can use the obtained bundle name to check whether the calling ability is allowed to receive the data you will send.
If you did not use Ability.startAbilityForResult(ohos.aafwk.content.Intent, int, ohos.aafwk.ability.startsetting.AbilityStartSetting) to start the calling
ability, null is returned.
getCallingBundle 在接口中 Contextnull if no calling ability is available.Context.getCallingBundle()public boolean stopAbility(Intent intent)
ContextAbilityInfo.AbilityType.SERVICE template.
The current ability using either the AbilityInfo.AbilityType.SERVICE or
AbilityInfo.AbilityType.PAGE template can call this method to destroy another ability
that uses the AbilityInfo.AbilityType.SERVICE template. The current ability itself can
be destroyed by calling the Context.terminateAbility() method.
stopAbility 在接口中 Contextintent - Indicates the Intent containing information about the ability to
destroy.true if the ability is destroyed successfully; returns false otherwise.Context.stopAbility(Intent)public void startAbility(Intent intent, int requestCode)
ContextAn ability using the AbilityInfo.AbilityType.SERVICE or
AbilityInfo.AbilityType.PAGE template uses this method to start a specific ability.
The system locates the target ability from installed abilities based on the value of the intent
parameter and then starts it. You can specify the ability to start using the intent parameter.
startAbility 在接口中 Contextintent - Indicates the Intent containing information about the target
ability to start.requestCode - Indicates the request code returned after the ability using the
AbilityInfo.AbilityType.PAGE template is started. You can define
the request code to identify the results returned by abilities. The value ranges from 0 to
65535. This parameter takes effect only on abilities using the
AbilityInfo.AbilityType.PAGE template.Context.startAbility(Intent, int)public void startAbility(Intent intent, AbilityStartSetting abilityStartSetting, IAbilityStartCallback callback)
ContextAn ability using the AbilityInfo.AbilityType.SERVICE or
AbilityInfo.AbilityType.PAGE template uses this method to start a specific ability.
The system locates the target ability from installed abilities based on the value of the intent
parameter and then starts it. You can specify the ability to start using the intent parameter.
startAbility 在接口中 Contextintent - Indicates the Intent containing information about the target
ability to start.abilityStartSetting - Indicates the special start setting used in starting ability.callback - Indcate the IAbilityStartCallback callback interface.Context.startAbility(Intent, AbilityStartSetting, IAbilityStartCallback)public void startAbility(Intent intent, int requestCode, AbilityStartSetting abilityStartSetting)
ContextAn ability using the AbilityInfo.AbilityType.SERVICE or
AbilityInfo.AbilityType.PAGE template uses this method to start a specific ability.
The system locates the target ability from installed abilities based on the value of the intent
parameter and then starts it. You can specify the ability to start using the intent parameter.
startAbility 在接口中 Contextintent - Indicates the Intent containing information about the target
ability to start.requestCode - Indicates the request code returned after the ability is started. You can define the
request code to identify the results returned by abilities. The value ranges from 0 to 65535.abilityStartSetting - Indicates the special start setting used in starting ability.Context.startAbility(Intent, int, AbilityStartSetting)public void startAbilities(Intent[] intents)
ContextAn ability using the AbilityInfo.AbilityType.SERVICE or
AbilityInfo.AbilityType.PAGE template uses this method to start specific abilities.
The system distinguishes target abilities from installed abilities based on the value of the intents
parameter and then starts the target abilities. You can use the intents parameter to specify target
abilities.
startAbilities 在接口中 Contextintents - Indicates an array of Intent objects containing information
about the target abilities.Context.startAbilities(Intent[])public Context createBundleContext(String bundleName, int flag)
ContextContext object for an application with the given bundle name.
The created Context object is the same as that obtained by the specified application after it is
loaded. They have the same resources and class loader. A new Context object is returned
each time this method is called.
createBundleContext 在接口中 ContextbundleName - Indicates the bundle name of the application.flag - Indicates the flag for creating a Context object. It can be 0, any of the following
values, or any combination of the following values: Context.CONTEXT_IGNORE_SECURITY,
Context.CONTEXT_INCLUDE_CODE, and Context.CONTEXT_RESTRICTED. The value 0 indicates that
there is no restriction on creating contexts for applications.Context object created for the specified application.Context.createBundleContext(String, int)public File getDataDir()
ContextgetDataDir 在接口中 ContextContext.getDataDir()public String getBundleResourcePath()
ContextgetBundleResourcePath 在接口中 ContextContext.getBundleResourcePath()public boolean canRequestPermission(String permission)
ContextYou need to call the current method to check whether a prompt is required before calling
Context.requestPermissionsFromUser(java.lang.String[],int) to request a permission. If a prompt is not required,
permission request will not be initiated.
canRequestPermission 在接口中 Contextpermission - Indicates the permission to be queried. This parameter cannot be null.true if the current application does not have the permission and the user does not
turn off further requests; returns false if the current application already has the permission,
the permission is rejected by the system, or the permission is denied by the user and the user has turned
off further requests.Context.canRequestPermission(String)public void requestPermissionsFromUser(String[] permissions, int requestCode)
ContextThis method is called for permission request. This is an asynchronous method. When it is executed, the
Ability.onRequestPermissionsFromUserResult(int, String[], int[]) method will be
called back.
requestPermissionsFromUser 在接口中 Contextpermissions - Indicates the list of permissions to be requested. This parameter cannot be null.requestCode - Indicates the request code to be passed to the
Ability.onRequestPermissionsFromUserResult(int, String[],
int[]) callback method. This code cannot be a negative number.Context.requestPermissionsFromUser(String[], int)public boolean connectAbility(Intent intent, IAbilityConnection conn)
ContextAbilityInfo.AbilityType.SERVICE
template.
This method can be called by an ability using either the
AbilityInfo.AbilityType.SERVICE or
AbilityInfo.AbilityType.PAGE template, but the destination of the connection must be an
ability using the AbilityInfo.AbilityType.SERVICE template. You must implement the
IAbilityConnection interface to obtain the proxy of the target ability after it is connected.
connectAbility 在接口中 Contextintent - Indicates the Intent containing information about the ability to
connect.conn - Indicates the callback object when the target ability is connected.true if the connection is successful; returns false otherwise.Context.connectAbility(Intent, IAbilityConnection)public void disconnectAbility(IAbilityConnection conn)
ContextAbilityInfo.AbilityType.SERVICE template.
This method can be called by an ability using either the
AbilityInfo.AbilityType.SERVICE or
AbilityInfo.AbilityType.PAGE template, but the destination of the connection to
disconnect must be an ability using the AbilityInfo.AbilityType.SERVICE template.
disconnectAbility 在接口中 Contextconn - Indicates the IAbilityConnection callback object passed by
Context.connectAbility(ohos.aafwk.content.Intent,ohos.aafwk.ability.IAbilityConnection) after the connection
is set up. The IAbilityConnection object uniquely identifies a connection between two
abilities.Context.disconnectAbility(IAbilityConnection)public void setDisplayOrientation(AbilityInfo.DisplayOrientation newOrientation)
ContextThe setting takes effect only for abilities using the AbilityInfo.AbilityType.PAGE
template.
setDisplayOrientation 在接口中 ContextnewOrientation - Indicates the new orientation for the current ability.Context.setDisplayOrientation(AbilityInfo.DisplayOrientation)public IBundleManager getBundleManager()
ContextIBundleManager instance.
You can use this instance to obtain information about the application bundle.
getBundleManager 在接口中 ContextIBundleManager instance.Context.getBundleManager()public String getBundleName()
ContextgetBundleName 在接口中 ContextContext.getBundleName()public String getBundleCodePath()
ContextThe returned path contains the resources, source code, and configuration files of a module.
getBundleCodePath 在接口中 ContextContext.getBundleCodePath()public File getCacheDir()
ContextThe system automatically deletes files from the cache directory if disk space is required elsewhere on the device. Older files are always deleted first.
getCacheDir 在接口中 ContextContext.getCacheDir()public File getCodeCacheDir()
ContextThe system will delete any files stored in this location both when your specific application is upgraded, and when the entire platform is upgraded.
getCodeCacheDir 在接口中 ContextContext.getCodeCacheDir()public File[] getExternalMediaDirs()
ContextThe system will delete all these files when your specific application is uninstalled.
getExternalMediaDirs 在接口中 ContextContext.getExternalMediaDirs()public File getNoBackupFilesDir()
ContextThe returned path maybe changed if the application is moved to an adopted storage device.
getNoBackupFilesDir 在接口中 ContextContext.getNoBackupFilesDir()public File getFilesDir()
ContextgetFilesDir 在接口中 ContextContext.getFilesDir()public File getDir(String name, int mode)
ContextYou can use the returned File object to create and access files in this directory. The files can be
accessible only by the current application.
getDir 在接口中 Contextname - Indicates the name of the directory to retrieve. This directory is created as part of your
application data.mode - Indicates the file operating mode. The value can be 0 or a combination of
Context.MODE_PRIVATE.File object for the requested directory.Context.getDir(String, int)public File getExternalCacheDir()
ContextThe application can place its cache files in this directory.
getExternalCacheDir 在接口中 Contextnull if the external or shared storage device is temporarily unavailable.Context.getExternalCacheDir()public File[] getExternalCacheDirs()
ContextThe application can place its cache files in these directories.
getExternalCacheDirs 在接口中 Contextnull if the external or shared storage devices are temporarily
unavailable. The first path returned is the same as that returned by Context.getExternalCacheDir().Context.getExternalCacheDirs()public File getExternalFilesDir(String type)
ContextThe application can place its persistent files in this directory.
getExternalFilesDir 在接口中 Contexttype - Indicates the type of the file directory to return. This parameter can be null for the following
directory or subdirectory: Environment.DIRECTORY_PICTURES,
Environment.DIRECTORY_DOCUMENTS,
Environment.DIRECTORY_DOWNLOADS,
Environment.DIRECTORY_MOVIES, or
Environment.DIRECTORY_MUSIC type.null if the external or shared storage device is temporarily unavailable.Context.getExternalFilesDir(String)public File[] getExternalFilesDirs(String type)
ContextThe application can place its persistent files in these directories.
getExternalFilesDirs 在接口中 Contexttype - Indicates the type of the file directories to return. This parameter can be null for the following
directory or subdirectory: Environment.DIRECTORY_PICTURES,
Environment.DIRECTORY_DOCUMENTS,
Environment.DIRECTORY_DOWNLOADS,
Environment.DIRECTORY_MOVIES, or
Environment.DIRECTORY_MUSIC type.null if the external or shared storage devices are temporarily unavailable.
The first path returned is the same as that returned by Context.getExternalFilesDir(java.lang.String).Context.getExternalFilesDirs(String)public boolean deleteFile(String fileName)
ContextdeleteFile 在接口中 ContextfileName - Indicates the name of the file to delete. The file name cannot contain path separators.true if the file is deleted successfully; returns false otherwise.Context.deleteFile(String)public IAbilityManager getAbilityManager()
ContextThe ability manager provides information about running processes and memory usage of an application.
getAbilityManager 在接口中 ContextIAbilityManager instance.Context.getAbilityManager()public int getDisplayOrientation()
ContextgetDisplayOrientation 在接口中 ContextContext.getDisplayOrientation()public void setShowOnLockScreen(boolean showOnLockScreen)
ContextsetShowOnLockScreen 在接口中 ContextshowOnLockScreen - Specifies whether to show this ability on top of the lock screen. The value true
means to show it on the lock screen, and the value false means not.Context.setShowOnLockScreen(boolean showOnLockScreen)public void setWakeUpScreen(boolean wakeUpScreen)
ContextThis method takes effect only on abilities visible to users after the screen is woken up.
setWakeUpScreen 在接口中 ContextwakeUpScreen - Specifies whether to wake up the screen. The value true means to wake it up,
and the value false means not.Context.setWakeUpScreen(boolean wakeUpScreen)public void restart()
ContextThis method takes effect only for abilities using the AbilityInfo.AbilityType.PAGE
template.
restart 在接口中 ContextContext.restart()public void setTransitionAnimation(int enterAnim,
int exitAnim)
ContextThis method must be called after Context.startAbility(ohos.aafwk.content.Intent,int) is called.
setTransitionAnimation 在接口中 ContextenterAnim - Indicates the resource ID of the animation for the incoming ability.exitAnim - Indicates the resource ID of the animation for the outgoing ability.Context.setTransitionAnimation(int enterAnim, int exitAnim)public boolean isUpdatingConfigurations()
ContextGenerally, such change information is used in Ability.onStop() for determining
how to handle resources referenced by this ability.
isUpdatingConfigurations 在接口中 Contexttrue if the configuration is changing, for example, if the display orientation of the
device is changing; returns false otherwise.Context.isUpdatingConfigurations()public void setTheme(int themeId)
ContextContext based on the specified theme ID.setTheme 在接口中 ContextthemeId - Indicates the resource ID of the theme to set.Context.setTheme(int themeId)public Theme getTheme()
ContextContext.
This method is valid only when a theme has been set by calling Context.setTheme(int).
getTheme 在接口中 ContextContext.Context.getTheme()public void setPattern(int patternId)
ContextContext based on the specified pattern ID.setPattern 在接口中 ContextpatternId - Indicates the resource ID of the pattern to set.Context.setPattern(int patternId)public Pattern getPattern()
ContextContext.
This method is valid only when a pattern has been set by calling Context.setPattern(int).
getPattern 在接口中 ContextContext.Context.getPattern()public String getAppType()
getAppType 在接口中 Contextsystem if this application is a system application;
returns normal if it is released in Ohos AppGallery;
returns other if it is released by a third-party vendor;
returns an empty string if the query fails.public ResourceManager getResourceManager(Configuration configuration)
ContextConfiguration object.getResourceManager 在接口中 Contextconfiguration - Indicates the Configuration object containing the resource configuration
information.ResourceManager object.Context.getResourceManager(ohos.global.configuration.Configuration configuration)public Object getLastStoredDataWhenConfigChanged()
ContextAbility.onStoreDataWhenConfigChange()
when the system configuration is changed.getLastStoredDataWhenConfigChanged 在接口中 ContextContext.getLastStoredDataWhenConfigChanged()public void printDrawnCompleted()
ContextprintDrawnCompleted 在接口中 ContextContext.printDrawnCompleted()public void compelVerifyPermission(String permission, String message)
ContextSecurityException if the permission is not granted.compelVerifyPermission 在接口中 Contextpermission - Indicates the permission to check.message - Indicates the information to be displayed in the SecurityEexception if it is thrown.Context.compelVerifyPermission(String permission, String message)public void compelVerifyUriPermission(Uri uri, int modeFlags, String message)
ContextSecurityException if it does not have the permission.compelVerifyUriPermission 在接口中 Contexturi - Indicates the URI to check. This parameter cannot be null.modeFlags - Indicates the access modes to check. The value can be
ohos.aafwk.content.Intent#FLAG_AUTH_READ_URI_PERMISSION,
ohos.aafwk.content.Intent#FLAG_AUTH_WRITE_URI_PERMISSION, or a combination of them.message - Indicates the information to be displayed in the SecurityEexception if it is thrown.Context.compelVerifyUriPermission(Uri uri, int modeFlags, String message)public void compelVerifyCallerPermission(String permission, String message)
ContextSecurityException if the permission is not granted.
This method also throws a SecurityException if you are currently not involved in an IPC.compelVerifyCallerPermission 在接口中 Contextpermission - Indicates the permission to check.message - Indicates the information to be displayed in the SecurityEexception if it is thrown.Context.compelVerifyCallerPermission(String permission, String message)public void compelVerifyCallerUriPermission(Uri uri, int modeFlags, String message)
ContextSecurityException if it does not have the permission.
This method also throws a SecurityException if you are currently not involved in an IPC.compelVerifyCallerUriPermission 在接口中 Contexturi - Indicates the URI to check. This parameter cannot be null.modeFlags - Indicates the access modes to check. The value can be
ohos.aafwk.content.Intent#FLAG_AUTH_READ_URI_PERMISSION,
ohos.aafwk.content.Intent#FLAG_AUTH_WRITE_URI_PERMISSION, or a combination of them.message - Indicates the information to be displayed in the SecurityEexception if it is thrown.Context.compelVerifyCallerUriPermission(Uri uri, int modeFlags, String message)public void compelVerifyPermission(String permission, int pid, int uid, String message)
Contextpid and uid,
and throws a SecurityException if the permission is not granted.compelVerifyPermission 在接口中 Contextpermission - Indicates the permission to check.pid - Indicates the ID of the process to check.uid - Indicates the UID of the process to check.message - Indicates the information to be displayed in the SecurityEexception if it is thrown.Context.compelVerifyPermission(String permission, int pid, int uid, String message)public void compelVerifyUriPermission(Uri uri, int pid, int uid, int modeFlags, String message)
Contextpid and uid has the permission to access
a specified URI, and throws a SecurityException if it does not have the permission.compelVerifyUriPermission 在接口中 Contexturi - Indicates the URI to check. This parameter cannot be null.pid - Indicates the ID of the process to check.uid - Indicates the UID of the process to check.modeFlags - Indicates the access modes to check. The value can be
ohos.aafwk.content.Intent#FLAG_AUTH_READ_URI_PERMISSION,
ohos.aafwk.content.Intent#FLAG_AUTH_WRITE_URI_PERMISSION, or a combination of them.message - Indicates the information to be displayed in the SecurityEexception if it is thrown.Context.compelVerifyUriPermission(Uri uri, int pid, int uid, int modeFlags, String message)public void compelVerifyUriPermission(Uri uri, String readPermission, String writePermission, int pid, int uid, int modeFlags, String message)
Contextpid and uid has specified read and write permissions
as well as the permission to access a specified URI, and throws a SecurityException if it does not have
the permissions.compelVerifyUriPermission 在接口中 Contexturi - Indicates the URI to check. This parameter cannot be null.readPermission - Indicates the read permission to check.writePermission - Indicates the write permission to check.pid - Indicates the ID of the process to check.uid - Indicates the UID of the process to check.modeFlags - Indicates the access modes to check. The value can be
ohos.aafwk.content.Intent#FLAG_AUTH_READ_URI_PERMISSION,
ohos.aafwk.content.Intent#FLAG_AUTH_WRITE_URI_PERMISSION, or a combination of them.message - Indicates the information to be displayed in the SecurityEexception if it is thrown.Context.compelVerifyUriPermission(Uri uri, String readPermission, String writePermission, int pid, int uid,
int modeFlags, String message)public int getColor(int resId)
ContextContext based on the specified resource ID.getColor 在接口中 ContextresId - Indicates the resource ID of the color to get.Context.Context.getColor(int resId)public String getString(int resId)
ContextContext based on the specified resource ID.getString 在接口中 ContextresId - Indicates the resource ID of the string to get.Context.Context.getString(int resId)public String getString(int resId, Object... formatArgs)
ContextContext based on the specified resource ID and formatArgs.getString 在接口中 ContextresId - Indicates the resource ID of the string to get.formatArgs - Indicates the formatted objects used for substitution.Context.Context.getString(int resId, Object... formatArgs)public String[] getStringArray(int resId)
ContextContext based on the specified resource ID.getStringArray 在接口中 ContextresId - Indicates the resource ID of the string array to get.Context.Context.getStringArray(int resId)public int[] getIntArray(int resId)
ContextContext based on the specified resource ID.getIntArray 在接口中 ContextresId - Indicates the resource ID of the integer array to get.Context.Context.getIntArray(int resId)public String getProcessName()
ContextgetProcessName 在接口中 ContextContext.getProcessName()public Context getAbilityPackageContext()
ContextContext object of this ability.
This method cannot be used to obtain the context of the application.
getAbilityPackageContext 在接口中 ContextContext object of this ability.Context.getAbilityPackageContext()public HapModuleInfo getHapModuleInfo()
ContextHapModuleInfo object of the application.getHapModuleInfo 在接口中 ContextHapModuleInfo object of the application.Context.getHapModuleInfo()public Uri getCaller()
ContextgetCaller 在接口中 ContextContext.getCaller()public int getThemeId()
ContextContext.getThemeId 在接口中 ContextContext.Context.getThemeId()public void setColorMode(int mode)
ContextsetColorMode 在接口中 Contextmode - the value of color mode.Context#setColorMode()public int getColorMode()
ContextgetColorMode 在接口中 ContextContext.getColorMode()