diff --git a/zh-cn/application-dev/reference/apis/js-apis-ability-context.md b/zh-cn/application-dev/reference/apis/js-apis-ability-context.md index 020beeb11908edf8cb8f32d6671add43815ba698..e55c684dc866fad4c8df94b1229cae6301b72f94 100644 --- a/zh-cn/application-dev/reference/apis/js-apis-ability-context.md +++ b/zh-cn/application-dev/reference/apis/js-apis-ability-context.md @@ -1,12 +1,48 @@ # AbilityContext +- [使用说明](#使用说明) +- [属性](#属性) +- [startAbility](#startAbility) +- [startAbility](#startAbility) +- [startAbility](#startAbility) +- [startAbilityForResult](#startAbilityForResult) +- [startAbilityForResult](#startAbilityForResult) +- [startAbilityForResult](#startAbilityForResult) +- [terminateSelf](#terminateSelf) +- [terminateSelf](#terminateSelf) +- [terminateSelfWithResult](#terminateSelfWithResult) +- [terminateSelfWithResult](#terminateSelfWithResult) +- [startAbilityByCall](#startAbilityByCall) +- [requestPermissionsFromUser](#requestPermissionsFromUser) +- [requestPermissionsFromUser](#requestPermissionsFromUser) +- [setMissionLabel](#setMissionLabel) +- [setMissionLabel](#setMissionLabel) + + > ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** -> 本模块首批接口从API 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 +> 本模块首批接口从API 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 Ability的上下文环境,继承自Context。 +## 使用说明 + + +​在使用AbilityContext的功能前,需要通过Ability子类实例获取。 + + + +``` +import Ability from '@ohos.application.Ability' +class MainAbility extends Ability { + onWindowStageCreate(windowStage) { + let context = this.context; + } +} +``` + + ## 属性 | 名称 | 参数类型 | 可读 | 可写 | 说明 | @@ -42,7 +78,37 @@ startAbility(want: Want, callback: AsyncCallback<void>): void ## startAbility -startAbility(want: Want): Promise<void>; +startAbility(want: Want, options: StartOptions, callback: AsyncCallback<void>): void + +启动Ability。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | want | [Want](js-apis-featureAbility.md#Want类型说明) | 是 | 启动Ability的want信息。 | + | options | StartOptions | 是 | 启动Ability所携带的参数。 | + | callback | AsyncCallback<void> | 是 | callback形式返回启动结果。 | + +- 示例: + + ``` + var want = { + "deviceId": "", + "bundleName": "com.extreme.test", + "abilityName": "com.extreme.test.MainAbility" + }; + var options = { + windowMode: 0, + }; + this.context.startAbility(want, options, (error) => { + console.log("error.code = " + error.code) + }) + ``` + + +## startAbility + +startAbility(want: Want, options: StartOptions): Promise<void>; 启动Ability。通过Promise返回结果。 @@ -50,6 +116,7 @@ startAbility(want: Want): Promise<void>; | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want | [Want](js-apis-featureAbility.md#Want类型说明) | 是 | 启动Ability的want信息。 | + | options | StartOptions | 是 | 启动Ability所携带的参数。 | - 返回值: | 类型 | 说明 | @@ -63,7 +130,10 @@ startAbility(want: Want): Promise<void>; "bundleName": "com.extreme.test", "abilityName": "com.extreme.test.MainAbility" }; - this.context.startAbility(want) + var options = { + windowMode: 0, + }; + this.context.startAbility(want, options) .then((data) => { console.log('Operation successful.') }).catch((error) => { @@ -82,7 +152,7 @@ startAbilityForResult(want: Want, callback: AsyncCallback<AbilityResult>): | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | want |[Want](js-apis-featureAbility.md#Want类型说明) | 是 | 启动Ability的want信息。 | - | callback | Callback<[AbilityResult](js-apis-featureAbility.md#abilityresult)> | 是 | 执行结果回调函数。 | + | callback | AsyncCallback<[AbilityResult](js-apis-featureAbility.md#abilityresult)> | 是 | 执行结果回调函数。 | - 示例: @@ -96,17 +166,47 @@ startAbilityForResult(want: Want, callback: AsyncCallback<AbilityResult>): ); ``` +## startAbilityForResult + +startAbilityForResult(want: Want, options: StartOptions, callback: AsyncCallback<AbilityResult>): void; + +启动Ability并在结束的时候返回执行结果。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | want |[Want](js-apis-featureAbility.md#Want类型说明) | 是 | 启动Ability的want信息。 | + | options | StartOptions | 是 | 启动Ability所携带的参数。 | + | callback | AsyncCallback<[AbilityResult](js-apis-featureAbility.md#abilityresult)> | 是 | 执行结果回调函数。 | + + +- 示例: + ``` + var options = { + windowMode: 0, + }; + this.context.startAbilityForResult( + {bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo2"}, options, + (error, result) => { + console.log("startAbilityForResult AsyncCallback is called, error.code = " + error.code) + console.log("startAbilityForResult AsyncCallback is called, result.resultCode = " + result.resultCode) + } + ); + ``` + ## startAbilityForResult -startAbilityForResult(want: Want): Promise<AbilityResult>; +startAbilityForResult(want: Want, options: StartOptions): Promise<AbilityResult>; 启动Ability并在结束的时候返回执行结果。 - 参数: | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | - | want | [Want](js-apis-featureAbility.md#Want类型说明) | 是 | 启动Ability的want信息。 | + | want | [Want](js-apis-featureAbility.md#Want类型说明) | 是 | 启动Ability的want信息。 | + | options | StartOptions | 是 | 启动Ability所携带的参数。 | + - 返回值 | 类型 | 说明 | @@ -115,7 +215,10 @@ startAbilityForResult(want: Want): Promise<AbilityResult>; - 示例: ``` - this.context.startAbilityForResult({bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo2"}).then((result) => { + var options = { + windowMode: 0, + }; + this.context.startAbilityForResult({bundleName: "com.extreme.myapplication", abilityName: "MainAbilityDemo2"}, options).then((result) => { console.log("startAbilityForResult Promise.resolve is called, result.resultCode = " + result.resultCode) }, (error) => { console.log("startAbilityForResult Promise.Reject is called, error.code = " + error.code) @@ -132,7 +235,7 @@ terminateSelf(callback: AsyncCallback<void>): void; - 参数: | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | - | callback | AsyncCallback<void> | 否 | 回调函数,返回接口调用是否成功的结果。 | + | callback | AsyncCallback<void> | 是 | 回调函数,返回接口调用是否成功的结果。 | - 示例: ``` @@ -173,7 +276,7 @@ terminateSelfWithResult(parameter: AbilityResult, callback: AsyncCallback<voi | 参数名 | 类型 | 必填 | 说明 | | -------- | -------- | -------- | -------- | | parameter | [AbilityResult](js-apis-featureAbility.md#abilityresult) | 是 | 返回给调用startAbilityForResult 接口调用方的相关信息。 | - | callback | Callback<void> | 否 | callback形式返回停止结果 | + | callback | AsyncCallback<void> | 是 | callback形式返回停止结果。 | - 示例: ``` @@ -202,7 +305,7 @@ terminateSelfWithResult(parameter: AbilityResult): Promise<void>; - 返回值: | 类型 | 说明 | | -------- | -------- | - | Promise<void> | promise形式返回停止结果 | + | Promise<void> | promise形式返回停止结果。 | - 示例: ``` @@ -215,3 +318,138 @@ terminateSelfWithResult(parameter: AbilityResult): Promise<void>; } ) ``` + + +## startAbilityByCall + +startAbilityByCall(want: Want): Promise<Caller>; + +获取指定通用组件服务端的caller通信接口, 并且将指定通用组件服务端拉起并切换到后台。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | want | [Want](js-apis-featureAbility.md#Want类型说明) | 是 | 传入需要启动的ability的信息,包含ability名称、包名、设备ID,设备ID缺省或为空表示启动本地ability。 | + +- 返回值: + | 类型 | 说明 | + | -------- | -------- | + | Promise<> | 获取要通讯的caller对象。 | + +- 示例: + + ``` + import Ability from '@ohos.application.Ability'; + var caller; + export default class MainAbility extends Ability { + onWindowStageCreate(windowStage) { + this.context.startAbilityByCall({ + bundleName: "com.example.myservice", + abilityName: "com.example.myservice.MainAbility", + deviceId: "" + }).then((obj) => { + caller = obj; + console.log('Caller GetCaller Get ' + call); + }).catch((e) => { + console.log('Caller GetCaller error ' + e); + }); + } + } + ``` + + +## requestPermissionsFromUser + +requestPermissionsFromUser(permissions: Array<string>, requestCallback: AsyncCallback<PermissionRequestResult>) : void; + +拉起弹窗请求用户授权。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | permissions | Array<string> | 是 | 权限列表。 | + | callback | AsyncCallback<[PermissionRequestResult](js-apis-permissionrequestresult.md)> | 是 | 回调函数,返回接口调用是否成功的结果。 | + +- 示例: + + ``` + this.context.requestPermissionsFromUser(permissions,(result) => { + console.log('requestPermissionsFromUserresult:' + JSON.stringfy(result)); + }); + ``` + + +## requestPermissionsFromUser + +requestPermissionsFromUser(permissions: Array<string>) : Promise<PermissionRequestResult>; + +拉起弹窗请求用户授权。通过Promise返回结果。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | permissions | Array<string> | 是 | 权限列表。 | + +- 返回值: + | 类型 | 说明 | + | -------- | -------- | + | Promise<[PermissionRequestResult](js-apis-permissionrequestresult.md)> | 返回一个Promise,包含接口的结果。 | + +- 示例: + + ``` + this.context.requestPermissionsFromUser(permissions).then((data) => { + console.log('success:' + JSON.stringfy(data)); + }).catch((error) => { + console.log('failed:' + JSON.stringfy(error)); + }); + ``` + + +## setMissionLabel + +setMissionLabel(label: string, callback:AsyncCallback<void>): void; + +设置ability在任务中显示的名称。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | label | string | 是 | 显示名称。 | + | callback | AsyncCallback<void> | 是 | 回调函数,返回接口调用是否成功的结果。 | + +- 示例: + + ``` + this.context.setMissionLabel("test",(result) => { + console.log('requestPermissionsFromUserresult:' + JSON.stringfy(result)); + }); + ``` + + +## setMissionLabel + +setMissionLabel(label: string, callback:AsyncCallback<void>): void; + +设置ability在任务中显示的名称。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | label | string | 是 | 显示名称。 | + +- 返回值: + | 类型 | 说明 | + | -------- | -------- | + | Promise<void> | 返回一个Promise,包含接口的结果。 | + +- 示例: + + ``` + this.context.setMissionLabel("test").then((data) => { + console.log('success:' + JSON.stringfy(data)); + }).catch((error) => { + console.log('failed:' + JSON.stringfy(error)); + }); + ``` + diff --git a/zh-cn/application-dev/reference/apis/js-apis-abilityrunninginfo.md b/zh-cn/application-dev/reference/apis/js-apis-abilityrunninginfo.md new file mode 100644 index 0000000000000000000000000000000000000000..d355f7b81a9450453072645310d519758b3a1ac3 --- /dev/null +++ b/zh-cn/application-dev/reference/apis/js-apis-abilityrunninginfo.md @@ -0,0 +1,52 @@ +# AbilityRunningInfo + +- [使用说明](#使用说明) +- [属性](#属性) +- [abilityManager.AbilityState](#abilityManagerAbilityState) + + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** +> 本模块首批接口从API 8开始支持。 + + +Ability运行相关信息。 + + +## 使用说明 + + +通过abilityManager中getAbilityRunningInfos方法获取。 + + + +``` +import abilitymanager from '@ohos.application.abilityManager'; +abilitymanager.getAbilityRunningInfos((err,data) => { + console.log("getAbilityRunningInfos err: " + err + " data: " + JSON.stringify(data)); +}); +``` + + +## 属性 + + | 名称 | 参数类型 | 可读 | 可写 | 说明 | +| -------- | -------- | -------- | -------- | -------- | +| ability | ElementName | 是 | 否 | Ability匹配信息。 | +| pid | number | 是 | 否 | 进程ID。 | +| uid | number | 是 | 否 | 用户ID。 | +| processName | string | 是 | 否 | 进程名称。 | +| startTime | number | 是 | 否 | Ability启动时间。 | +| abilityState | [abilityManager.AbilityState](#abilitymanager-abilitystate) | 是 | 否 | Ability状态。 | + + +## abilityManager.AbilityState + +Ability的状态信息。 + + | 名称 | 值 | 说明 | +| -------- | -------- | -------- | +| INITIAL | 0 | 表示ability为initial状态。 | +| FOREGROUND | 9 | 表示ability为foreground状态。 | +| BACKGROUND | 10 | 表示ability为background状态。 | +| FOREGROUNDING | 11 | 表示ability为foregrounding状态。 | +| BACKGROUNDING | 12 | 表示ability为backgrounding状态。 | diff --git a/zh-cn/application-dev/reference/apis/js-apis-abilitystagecontext.md b/zh-cn/application-dev/reference/apis/js-apis-abilitystagecontext.md new file mode 100644 index 0000000000000000000000000000000000000000..8018f3b36e3f696d2de71ac4f1b2ce7486cadfe0 --- /dev/null +++ b/zh-cn/application-dev/reference/apis/js-apis-abilitystagecontext.md @@ -0,0 +1,36 @@ +# AbilityStageContext + +- [使用说明](#使用说明) +- [属性](#属性) + + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** +> 本模块首批接口从API 9开始支持。 + + +AbilityStage的上下文环境,继承自[Context](js-apis-application-context.md)。 + + +## 使用说明 + + +通过AbilityStage实例来获取。 + + + +``` +import AbilityStage from '@ohos.application.AbilityStage'; +class MyAbilityStage extends AbilityStage { + onCreate() { + let abilityStageContext = this.context; + } +} +``` + + +## 属性 + + | 名称 | 参数类型 | 可读 | 可写 | 说明 | +| -------- | -------- | -------- | -------- | -------- | +| currentHapModuleInfo | HapModuleInfo | 是 | 否 | AbilityStage对应的ModuleInfo对象。 | +| config | [Configuration](js-apis-configuration.md) | 是 | 否 | 环境变化对象。 | diff --git a/zh-cn/application-dev/reference/apis/js-apis-application-ability.md b/zh-cn/application-dev/reference/apis/js-apis-application-ability.md new file mode 100644 index 0000000000000000000000000000000000000000..02f6373ad19beb503e915562a2ac976ac54275eb --- /dev/null +++ b/zh-cn/application-dev/reference/apis/js-apis-application-ability.md @@ -0,0 +1,522 @@ +# Ability + +- [导入模块](#导入模块) +- [属性](#属性) +- [onCreate](#onCreate) +- [onWindowStageCreate](#onWindowStageCreate) +- [onWindowStageDestroy](#onWindowStageDestroy) +- [onWindowStageRestore](#onWindowStageRestore) +- [onDestroy](#onDestroy) +- [onForeground](#onForeground) +- [onBackground](#onBackground) +- [onContinue](#onContinue) +- [onNewWant](#onNewWant) +- [onConfigurationUpdated](#onConfigurationUpdated) +- [Caller](#Caller) + - [call](#call) + - [callWithResult](#callWithResult) + - [release](#release) + - [onRelease](#onRelease) +- [Callee](#Callee) + - [on](#on) + - [off](#off) + + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** +> 从API Version 8 开始支持。 + + +Ability模块,提供对Ability生命周期、上下文环境等调用管理。 + + +## 导入模块 + + +``` +import Ability from '@ohos.application.Ability'; +``` + + +## 属性 + + | 名称 | 参数类型 | 可读 | 可写 | 说明 | +| -------- | -------- | -------- | -------- | -------- | +| context | [AbilityContext](js-apis-ability-context.md) | 是 | 否 | 上下文。 | +| launchWant | [Want](js-apis-featureAbility.md#Want类型说明) | 是 | 否 | Ability启动时的参数。 | +| lastRequestWant | [Want](js-apis-featureAbility.md#Want类型说明) | 是 | 否 | Ability最后请求时的参数。 | + + +## onCreate + +onCreate(want: Want,param:LaunchParam): void + +Ability创建时回调,执行初始化业务逻辑操作。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | want | [Want](js-apis-featureAbility.md#Want类型说明) | 是 | 当前Ability的Want类型信息,包括ability名称、bundle名称等。 | + | param | LaunchParam | 是 | 创建 ability、上次异常退出的原因信息。 | + +- 示例: + + ``` + class myAbility extends Ability { + onCreate(want, param) { + console.log('onCreate, want:' + want.abilityName); + } + } + ``` + + +## onWindowStageCreate + +onWindowStageCreate(windowStage: window.WindowStage): void + +当WindowStage创建后调用。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | windowStage | window.WindowStage | 是 | WindowStage相关信息。 | + +- 示例: + + ``` + class myAbility extends Ability { + onWindowStageCreate(windowStage) { + console.log('onWindowStageCreate'); + } + } + ``` + + +## onWindowStageDestroy + +onWindowStageDestroy(): void + +当WindowStage销毁后调用。 + +- 示例: + + ``` + class myAbility extends Ability { + onWindowStageDestroy() { + console.log('onWindowStageDestroy'); + } + } + ``` + + +## onWindowStageRestore + +onWindowStageRestore(windowStage: window.WindowStage): void + +当迁移多实例ability时,恢复WindowStage后调用。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | windowStage | window.WindowStage | 是 | WindowStage相关信息。 | + +- 示例: + + ``` + class myAbility extends Ability { + onWindowStageRestore(windowStage) { + console.log('onWindowStageRestore'); + } + } + ``` + + +## onDestroy + +onDestroy(): void; + +Ability生命周期回调,在销毁时回调,执行资源清理等操作。 + +- 示例: + + ``` + class myAbility extends Ability { + onDestroy() { + console.log('onDestroy'); + } + } + ``` + + +## onForeground + +onForeground(): void; + +Ability生命周期回调,当应用处于前台时触发。 + +- 示例: + + ``` + class myAbility extends Ability { + onForeground() { + console.log('onForeground'); + } + } + ``` + + +## onBackground + +onBackground(): void; + +Ability生命周期回调,当应用处于后台时触发。 + +- 示例: + + ``` + class myAbility extends Ability { + onBackground() { + console.log('onBackground'); + } + } + ``` + + +## onContinue + +onContinue(wantParam : {[key: string]: any}): boolean; + +当ability迁移准备迁移时触发,保存数据。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | wantParam | {[key: string]: any} | 是 | want相关参数。 | + +- 返回值: + | 类型 | 说明 | + | -------- | -------- | + | boolean | true表示同意迁移,false表示拒绝迁移。 | + +- 示例: + + ``` + class myAbility extends Ability { + onContinue(wantParams) { + console.log('onContinue'); + wantParams["myData"] = "my1234567"; + return true; + } + } + ``` + + +## onNewWant + +onNewWant(want: Want): void; + +当ability的启动模式设置为单例时回调会被调用。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | want | [Want](js-apis-featureAbility.md#Want类型说明) | 是 | Want类型参数,如ability名称,包名等。 | + +- 示例: + + ``` + class myAbility extends Ability { + onNewWant(want) { + console.log('onNewWant, want:' + want.abilityName); + } + } + ``` + + +## onConfigurationUpdated + +onConfigurationUpdated(config: Configuration): void; + +当系统配置更新时调用。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | config | [Configuration](#section188911144124715) | 是 | 表示需要更新的配置信息。 | + +- 示例: + + ``` + class myAbility extends Ability { + onConfigurationUpdated(config) { + console.log('onConfigurationUpdated, config:' + JSON.stringify(config)); + } + } + ``` + + +## Caller + +通用组件Caller通信客户端调用接口, 用来向通用组件服务端发送约定数据。 + + +### call + +call(method, data: rpc.Sequenceable): Promise<void>; + +向通用组件服务端发送约定序列化数据。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | method | string | 是 | 约定的服务端注册事件字符串。 | + | data | rpc.Sequenceable | 是 | 由开发者实现的Sequenceable可序列化数据。 | + +- 返回值: + | 类型 | 说明 | + | -------- | -------- | + | Promise<void> | Promise形式返回应答。 | + +- 示例: + + ``` + import Ability from '@ohos.application.Ability'; + class MyMessageAble{ // 自定义的Sequenceable数据结构 + num: 0 + str: '' + constructor() {} + marshalling(messageParcel) { + messageParcel.writeInt(this.num); + messageParcel.writeString(this.str); + console.log('MyMessageAble marshalling num[' + this.num + '] str[' + this.str + ']'); + return true; + } + unmarshalling(messageParcel) { + this.num = messageParcel.readInt(); + this.str = messageParcel.readString(); + console.log('MyMessageAble unmarshalling num[' + this.num + '] str[' + this.str + ']'); + return true; + } + }; + var method = 'call_Function'; // 约定的通知消息字符串 + var caller; + export default class MainAbility extends Ability { + onWindowStageCreate(windowStage) { + caller = await this.context.startAbilityByCall({ + bundleName: "com.example.myservice", + abilityName: "com.example.myservice.MainAbility", + deviceId: "" + }); + let msg = new MyMessageAble(1, "world"); // 参考Sequenceable数据定义 + caller.call(method, msg) + .then(() => { + console.log('Caller call() called'); + }).catch((e) => { + console.log('Caller call() catch error ' + e); + }); + } + } + ``` + + +### callWithResult + +callWithResult(method, data: rpc.Sequenceable): Promise<rpc.MessageParcel>; + +向通用组件服务端发送约定序列化数据, 并将服务端返回的约定序列化数据带回。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | method | string | 是 | 约定的服务端注册事件字符串。 | + | data | rpc.Sequenceable | 是 | 由开发者实现的Sequenceable可序列化数据。 | + +- 返回值: + | 类型 | 说明 | + | -------- | -------- | + | Promise<rpc.MessageParcel> | Promise形式返回通用组件服务端应答数据。 | + +- 示例: + + ``` + import Ability from '@ohos.application.Ability'; + class MyMessageAble{ + num: 0 + str: '' + constructor() {} + marshalling(messageParcel) { + messageParcel.writeInt(this.num); + messageParcel.writeString(this.str); + console.log('MyMessageAble marshalling num[' + this.num + '] str[' + this.str + ']'); + return true; + } + unmarshalling(messageParcel) { + this.num = messageParcel.readInt(); + this.str = messageParcel.readString(); + console.log('MyMessageAble unmarshalling num[' + this.num + '] str[' + this.str + ']'); + return true; + } + }; + var method = 'call_Function'; + var caller; + export default class MainAbility extends Ability { + onWindowStageCreate(windowStage) { + caller = await this.context.startAbilityByCall({ + bundleName: "com.example.myservice", + abilityName: "com.example.myservice.MainAbility", + deviceId: "" + }); + let msg = new MyMessageAble(1, "world"); + caller.callWithResult(method, msg) + .then((data) => { + console.log('Caller call() called'); + let retmsg = new MyMessageAble(0, ""); + data.readSequenceable(retmsg); + }).catch((e) => { + console.log('Caller call() catch error ' + e); + }); + } + } + ``` + + +### release + +release(): void; + +主动释放通用组件服务端的通信接口。 + +- 示例: + + ``` + import Ability from '@ohos.application.Ability'; + var caller; + export default class MainAbility extends Ability { + onWindowStageCreate(windowStage) { + caller = await this.context.startAbilityByCall({ + bundleName: "com.example.myservice", + abilityName: "com.example.myservice.MainAbility", + deviceId: "" + }); + try { + caller.release(); + } catch (e) { + console.log('Caller Release error ' + e); + } + } + } + ``` + + +### onRelease + +onRelease(callback: function): void; + +注册通用组件服务端Stub断开监听通知。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | callback | function | 是 | 返回onRelease回调结果。 | + +- 示例: + + ``` + import Ability from '@ohos.application.Ability'; + var caller; + export default class MainAbility extends Ability { + onWindowStageCreate(windowStage) { + caller = await this.context.startAbilityByCall({ + bundleName: "com.example.myservice", + abilityName: "com.example.myservice.MainAbility", + deviceId: "" + }); + try { + caller.onRelease((str) => { + console.log(' Caller OnRelease CallBack is called ' + str); + }); + } catch (e) { + console.log('Caller Release error ' + e); + } + } + } + ``` + + +## Callee + +通用组件服务端注册和解除客户端caller通知送信的callback接口。 + + +### on + +on(method: string, callback: function): void; + +通用组件服务端注册消息通知callback。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | method | string | 是 | 与客户端约定的通知消息字符串。 | + | callback | function | 是 | 一个rpc.MessageParcel类型入参的js通知同步回调函数, 回调函数至少要返回一个空的rpc.Sequenceable数据对象, 其他视为函数执行错误。 | + +- 示例: + + ``` + import Ability from '@ohos.application.Ability'; + class MyMessageAble{ + num: 0 + str: '' + constructor() {} + marshalling(messageParcel) { + messageParcel.writeInt(this.num); + messageParcel.writeString(this.str); + console.log('MyMessageAble marshalling num[' + this.num + '] str[' + this.str + ']'); + return true; + } + unmarshalling(messageParcel) { + this.num = messageParcel.readInt(); + this.str = messageParcel.readString(); + console.log('MyMessageAble unmarshalling num[' + this.num + '] str[' + this.str + ']'); + return true; + } + }; + var method = 'call_Function'; + function funcCallBack(pdata) { + console.log('Callee funcCallBack is called ' + pdata); + let msg = new MyMessageAble(0, ""); + pdata.readSequenceable(msg); + return new MyMessageAble(10, "Callee test"); + } + export default class MainAbility extends Ability { + onCreate(want, launchParam) { + console.log('Callee onCreate is called'); + this.callee.on(method, funcCallBack); + } + } + ``` + + +### off + +off(method: string): void; + +解除通用组件服务端注册消息通知callback。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | method | string | 是 | 已注册的通知事件字符串。 | + +- 示例: + + ``` + import Ability from '@ohos.application.Ability'; + var method = 'call_Function'; + export default class MainAbility extends Ability { + onCreate(want, launchParam) { + console.log('Callee onCreate is called'); + this.callee.off(method); + } + } + ``` + diff --git a/zh-cn/application-dev/reference/apis/js-apis-application-abilitystage.md b/zh-cn/application-dev/reference/apis/js-apis-application-abilitystage.md new file mode 100644 index 0000000000000000000000000000000000000000..81409eb8908b0b0eb474c448bc3c706c42bc0b8e --- /dev/null +++ b/zh-cn/application-dev/reference/apis/js-apis-application-abilitystage.md @@ -0,0 +1,89 @@ +# AbilityStage + +- [导入模块](#导入模块) +- [onCreate](#onCreate) +- [onAcceptWant](#onAcceptWant) +- [onConfigurationUpdated](#onConfigurationUpdated) + + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** +> 本模块首批接口从API 9开始支持。 + + +AbilityStage是HAP包的运行时类。在HAP加载的时候,通知开发者,开发者可以在此进行该HAP的初始化(如资源预加载,线程创建等)。 + + +## 导入模块 + + +``` +import AbilityStage from '@ohos.application.AbilityStage'; +``` + + +## onCreate + +onCreate(): void + +当应用创建时调用。 + +- 示例 + + ``` + class MyAbilityStage extends AbilityStage { + onCreate() { + console.log("MyAbilityStage.onCreate is called") + } + } + ``` + + +## onAcceptWant + +onAcceptWant(want: Want): string; + +启动一个specified ability时触发的事件。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | want | [Want](js-apis-featureAbility.md#Want类型说明) | 是 | Want类型参数,传入需要启动的ability的信息,如ability名称,包名等。 | + +- 返回值: + | 类型 | 说明 | + | -------- | -------- | + | string | 用户返回一个ability标识,如果之前启动过次标识的ability,不创建新的实例并拉回栈顶,否则创建新的实例并启动。 | + +- 示例 + + ``` + class MyAbilityStage extends AbilityStage { + onAcceptWant(want) { + console.log("MyAbilityStage.onAcceptWant called"); + return "com.example.test"; + } + } + ``` + + +## onConfigurationUpdated + +onConfigurationUpdated(config: Configuration): void; + +环境变化通知接口,发生全局配置变更时回调。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | config | [Configuration](js-apis-configuration.md) | 是 | 发生全局配置变更时触发回调,当前全局配置包括系统语言、深浅色模式。 | + +- 示例: + + ``` + class MyAbilityStage extends AbilityStage { + onConfigurationUpdated(config) { + console.log('onConfigurationUpdated, language:' + config.language); + } + } + ``` + diff --git a/zh-cn/application-dev/reference/apis/js-apis-application-context.md b/zh-cn/application-dev/reference/apis/js-apis-application-context.md new file mode 100644 index 0000000000000000000000000000000000000000..ab48a6906570fd1a53415ae5e0add91269c97f85 --- /dev/null +++ b/zh-cn/application-dev/reference/apis/js-apis-application-context.md @@ -0,0 +1,78 @@ +# Context + +- [使用说明](#使用说明) +- [属性](#属性) +- [createBundleContext](#createBundleContext) +- [getApplicationContext](#getApplicationContext) + + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** +> 本模块首批接口从API version 9开始支持。 + + +提供开发者运行代码的上下文环境,包括应用信息、ResourceManager等信息。 + + +## 使用说明 + + +通过AbilityContext等集成实现。 + + +## 属性 + + | 名称 | 参数类型 | 可读 | 可写 | 说明 | +| -------- | -------- | -------- | -------- | -------- | +| resourceManager | ResourceManager | 是 | 否 | ResourceManager对象。 | +| applicationInfo | ApplicationInfo | 是 | 否 | 当前应用信息。 | +| cacheDir | string | 是 | 否 | 应用在内部存储上的缓存路径。 | +| tempDir | string | 是 | 否 | 应用的临时文件路径。 | +| filesDir | string | 是 | 否 | 应用在内部存储上的文件路径。 | +| databaseDir | string | 是 | 否 | 获取本地数据存储路径。 | +| storageDir | string | 是 | 否 | 获取轻量级数据存储路径。 | +| bundleCodeDir | string | 是 | 否 | 应用安装路径。 | +| distributedFilesDir | string | 是 | 否 | 应用的分布式文件路径。 | +| eventHub | [EventHub](js-apis-eventhub.md) | 是 | 否 | 事件中心信息。| + + +## createBundleContext + +createBundleContext(bundleName: string): Context; + +创建指定应用上下文。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | bundleName | string | 是 | 应用bundle名。 | + +- 返回值: + | 类型 | 说明 | + | -------- | -------- | + | Context | 对应创建应用的上下文context。 | + +- 示例: + + ``` + let test = "com.huawei.test"; + let context = this.context.createBundleContext(test); + ``` + + +## getApplicationContext + +getApplicationContext(): Context; + +获取当前context。 + +- 返回值: + | 类型 | 说明 | + | -------- | -------- | + | Context | 当前Context 信息。 | + +- 示例: + + ``` + // 必选项。 + let context = this.context.getApplicationContext(); + ``` diff --git a/zh-cn/application-dev/reference/apis/js-apis-appmanager.md b/zh-cn/application-dev/reference/apis/js-apis-appmanager.md new file mode 100644 index 0000000000000000000000000000000000000000..795b19aa7713cb4479302563edbfe85e1e0c2ad9 --- /dev/null +++ b/zh-cn/application-dev/reference/apis/js-apis-appmanager.md @@ -0,0 +1,64 @@ +# appManager + +- [导入模块](#导入模块) +- [isRunningInStabilityTest](#isRunningInStabilityTest) +- [isRunningInStabilityTest](#isRunningInStabilityTest) + + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** +> 本模块首批接口从API 8开始支持。 + + +App运行相关信息。 + + +## 导入模块 + + +``` +import app from '@ohos.application.appManager'; +``` + + +## isRunningInStabilityTest + +static isRunningInStabilityTest(callback: AsyncCallback<boolean>): void + +查询当前用户是否是一个稳定性测试。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | callback | AsyncCallback<boolean> | 否 | 返回当前是否处于稳定性测试场景。 | + +- 示例: + + ``` + import app from '@ohos.application.appManager'; + app.isRunningInStabilityTest((err, flag) => { + console.log('startAbility result:' + JSON.stringfy(err); + } + ``` + + +## isRunningInStabilityTest + +static isRunningInStabilityTest(): Promise<boolean> + +查询当前是否处于稳定性测试场景。 + +- 返回值: + | 类型 | 说明 | + | -------- | -------- | + | Promise<boolean> | 返回当前是否处于稳定性测试场景。 | + +- 示例: + + ``` + import app from '@ohos.application.appManager'; + app.isRunningInStabilityTest().then((flag) => { + console.log('success:' + JSON.stringfy(flag)); + )).catch((error) => { + console.log('failed:' + JSON.stringfy(error)); + }); + ``` diff --git a/zh-cn/application-dev/reference/apis/js-apis-configuration.md b/zh-cn/application-dev/reference/apis/js-apis-configuration.md new file mode 100644 index 0000000000000000000000000000000000000000..d35dfa4b321e40d8c028f07b78eb179f0cfb2291 --- /dev/null +++ b/zh-cn/application-dev/reference/apis/js-apis-configuration.md @@ -0,0 +1,30 @@ +# Configuration + +- [导入模块](#导入模块) +- [属性](#属性) + + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** +> 本模块首批接口从API version 8开始支持。 + + +环境变化信息。 + + +## 导入模块 + + +``` +import Configuration from '@ohos.application.Configuration'; +``` + + +## 属性 + + | 名称 | 参数类型 | 可读 | 可写 | 说明 | +| -------- | -------- | -------- | -------- | -------- | +| language | string | 是 | 是 | 表示应用程序的当前语言。 | +| colorMode | [ColorMode](js-apis-configurationconstant.md) | 是 | 是 | 表示深浅色模式,取值范围:浅色模式(COLOR_MODE_LIGHT),深色模式(COLOR_MODE_DARK)。默认为浅色。 | +| direction | Direction | 是 | 否 | 表示屏幕方向,取值范围:水平方向(DIRECTION_HORIZONTAL),垂直方向(DIRECTION_VERTICAL)。 | +| screenDensity | ScreenDensity | 是 | 否 | 表示屏幕分辨率,取值范围:SCREEN_DENSITY_SDPI(120)、SCREEN_DENSITY_MDPI(160)、SCREEN_DENSITY_LDPI(240)、SCREEN_DENSITY_XLDPI(320)、SCREEN_DENSITY_XXLDPI(480)、SCREEN_DENSITY_XXXLDPI(640)。 | +| displayId | number | 是 | 否 | 表示应用所在的displayId。 | diff --git a/zh-cn/application-dev/reference/apis/js-apis-configurationconstant.md b/zh-cn/application-dev/reference/apis/js-apis-configurationconstant.md new file mode 100644 index 0000000000000000000000000000000000000000..41a55a9f52860d13b74d0eac845ec499864848ac --- /dev/null +++ b/zh-cn/application-dev/reference/apis/js-apis-configurationconstant.md @@ -0,0 +1,61 @@ +# ConfigurationConstant + +- [导入模块](#导入模块) +- [ColorMode](#ColorMode) +- [Direction](#Direction) +- [ScreenDensity](#ScreenDensity) + + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** +> 本模块首批接口从API version 8开始支持。 + + +配置信息枚举值定义。 + + +## 导入模块 + + +``` +import ConfigurationConstant from '@ohos.application.ConfigurationConstant'; +``` + + +## ColorMode + +使用时通过ConfigurationConstant.ColorMode获取,示例:ConfigurationConstant.ColorMode.COLOR_MODE_LIGHT。 + + + | 名称 | 值 | 说明 | +| -------- | -------- | -------- | +| COLOR_MODE_NOT_SET | -1 | 未设置颜色模式。 | +| COLOR_MODE_DARK | 0 | 深色模式。 | +| COLOR_MODE_LIGHT | 1 | 浅色模式。 | + + +## Direction + +使用时通过ConfigurationConstant.Direction获取,示例:ConfigurationConstant.Direction.DIRECTION_VERTICAL。 + + + | 名称 | 值 | 说明 | +| -------- | -------- | -------- | +| DIRECTION_NOT_SET | -1 | 未设置方向。 | +| DIRECTION_VERTICAL | 0 | 垂直方向。 | +| DIRECTION_HORIZONTAL | 1 | 水平方向。 | + + +## ScreenDensity + +使用时通过ConfigurationConstant.ScreenDensity获取,示例:ConfigurationConstant.ScreenDensity.SCREEN_DENSITY_NOT_SET。 + + + | 名称 | 值 | 说明 | +| -------- | -------- | -------- | +| SCREEN_DENSITY_NOT_SET | 0 | 未设置屏幕分辨率。 | +| SCREEN_DENSITY_SDPI | 120 | 屏幕分辨率为"sdpi"。 | +| SCREEN_DENSITY_MDPI | 160 | 屏幕分辨率为"mdpi"。 | +| SCREEN_DENSITY_LDPI | 240 | 屏幕分辨率为"ldpi"。 | +| SCREEN_DENSITY_XLDPI | 320 | 屏幕分辨率为"xldpi"。 | +| SCREEN_DENSITY_XXLDPI | 480 | 屏幕分辨率为"xxldpi"。 | +| SCREEN_DENSITY_XXXLDPI | 640 | 屏幕分辨率为"xxxldpi"。 | diff --git a/zh-cn/application-dev/reference/apis/js-apis-eventhub.md b/zh-cn/application-dev/reference/apis/js-apis-eventhub.md new file mode 100644 index 0000000000000000000000000000000000000000..265e5f01827e5b0b9a74747fcae4f6e1027396a3 --- /dev/null +++ b/zh-cn/application-dev/reference/apis/js-apis-eventhub.md @@ -0,0 +1,137 @@ +# EventHub + +- [使用说明](#使用说明) +- [on](#on) +- [off](#off) +- [emit](#emit) + + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** +> 本模块首批接口从API 9开始支持。 + + +事件中心。提供订阅、取消订阅、触发事件能力。 + + +## 使用说明 + + +​在使用eventHub的功能前,需要通过Ability实例的成员变量context获取。 + + + +``` +import Ability from '@ohos.application.Ability' +export default class MainAbility extends Ability { + onForeground() { + this.context.eventHub.on("123", this.func1); + } +} +``` + + +## on + +on(event: string, callback: Function): void; + +订阅指定事件。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | event | string | 是 | 事件名称。 | + | callback | Function | 是 | 事件回调,事件触发后运行。 | + +- 示例: + + ``` + import Ability from '@ohos.application.Ability' + + export default class MainAbility extends Ability { + onForeground() { + this.context.eventHub.on("123", this.func1); + this.context.eventHub.on("123", () => { + console.log("call anonymous func 1"); + }); + // 结果: + // func1 is called + // call anonymous func 1 + this.context.eventHub.emit("123"); + } + func1() { + console.log("func1 is called"); + } + } + ``` + + +## off + +off(event: string, callback?: Function): void; + +取消订阅指定事件。当callback传值时,取消订阅指定的callback;未传值时,取消订阅该事件下所有callback。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | event | string | 是 | 事件名称。 | + | callback | Function | 否 | 事件回调。如果不传callback,则取消订阅该事件下所有callback。 | + +- 示例: + + ``` + import Ability from '@ohos.application.Ability' + + export default class MainAbility extends Ability { + onForeground() { + this.context.eventHub.on("123", this.func1); + this.context.eventHub.off("123", this.func1); //取消订阅func1 + this.context.eventHub.on("123", this.func1); + this.context.eventHub.on("123", this.func2); + this.context.eventHub.off("123"); //取消订阅func1和func2 + } + func1() { + console.log("func1 is called"); + } + func2() { + console.log("func2 is called"); + } + } + ``` + + +## emit + +emit(event: string, ...args: Object[]): void; + +触发指定事件。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | event | string | 是 | 事件名称。 | + | ...args | Object[] | 是 | 可变参数,事件触发时,传递给回调函数的参数。 | + +- 示例: + + ``` + import Ability from '@ohos.application.Ability' + + export default class MainAbility extends Ability { + onForeground() { + this.context.eventHub.on("123", this.func1); + // 结果: + // func1 is called,undefined,undefined + this.context.eventHub.emit("123"); + // 结果: + // func1 is called,1,undefined + this.context.eventHub.emit("123", 1); + // 结果: + // func1 is called,1,2 + this.context.eventHub.emit("123", 1, 2); + } + func1(a: string, b: string) { + console.log("func1 is called," + a + "," + b); + } + } + ``` diff --git a/zh-cn/application-dev/reference/apis/js-apis-extensionrunninginfo.md b/zh-cn/application-dev/reference/apis/js-apis-extensionrunninginfo.md new file mode 100644 index 0000000000000000000000000000000000000000..aede4031e5fad55e705dc0f382bf10387c18dd3e --- /dev/null +++ b/zh-cn/application-dev/reference/apis/js-apis-extensionrunninginfo.md @@ -0,0 +1,58 @@ +# ExtensionRunningInfo + +- [使用说明](#使用说明) + - [属性](#属性) +- [bundle.ExtensionAbilityType](#bundleExtensionAbilityType) + + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** +> 本模块首批接口从API 8开始支持。 + + +Extension运行相关信息。 + + +## 使用说明 + + +通过abilityManager中方法获取。 + + + +``` +import abilitymanager from '@ohos.application.abilityManager'; +abilitymanager.getExtensionRunningInfos(upperLimit, (err,data) => { + console.log("getExtensionRunningInfos err: " + err + " data: " + JSON.stringify(data)); +}); +``` + + +### 属性 + + | 名称 | 参数类型 | 可读 | 可写 | 说明 | +| -------- | -------- | -------- | -------- | -------- | +| extension | ElementName | 是 | 否 | Extension匹配信息。 | +| pid | number | 是 | 否 | 进程ID。 | +| uid | number | 是 | 否 | 用户ID。 | +| processName | string | 是 | 否 | 进程名称。 | +| startTime | number | 是 | 否 | Extension启动时间。 | +| clientPackage | Array<String> | 是 | 否 | 连接客户端包名。 | +| type | [bundle.ExtensionAbilityType](#bundle-extensionabilitytype) | 是 | 否 | Extension类型。 | + + +## bundle.ExtensionAbilityType + +Extension类型。 + + | 名称 | 值 | 说明 | +| -------- | -------- | -------- | +| FORM | 0 | 表示带有form类型的扩展信息。 | +| WORK_SCHEDULER | 1 | 表示带有work schedule类型的扩展信息。 | +| INPUT_METHOD | 2 | 表示用输入法类型的扩展信息。 | +| SERVICE | 3 | 表示带有service类型的扩展信息。 | +| ACCESSIBILITY | 4 | 表示具有可访问性类型的扩展信息。 | +| DATA_SHARE | 5 | 表示带有datashare类型的扩展信息。 | +| FILE_SHARE | 6 | 表示带有fileshare类型的扩展信息。 | +| STATIC_SUBSCRIBER | 7 | 表示带有静态订阅者类型的扩展信息。 | +| WALLPAPER | 8 | 表示带有wallpaper类型的扩展信息。 | +| UNSPECIFIED | 9 | 表示未指定类型信息。 | diff --git a/zh-cn/application-dev/reference/apis/js-apis-permissionrequestresult.md b/zh-cn/application-dev/reference/apis/js-apis-permissionrequestresult.md new file mode 100644 index 0000000000000000000000000000000000000000..65317e07f3e3b68c887fec2687701407ddd19386 --- /dev/null +++ b/zh-cn/application-dev/reference/apis/js-apis-permissionrequestresult.md @@ -0,0 +1,18 @@ +# PermissionRequestResult + +- [属性](#属性) + + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** +> 本模块首批接口从API 9开始支持。 + + +权限请求结果。 + + +## 属性 + + | 名称 | 参数类型 | 可读 | 可写 | 说明 | +| -------- | -------- | -------- | -------- | -------- | +| permissions | Array<string> | 是 | 否 | 用户传入的权限。 | +| authResults | Array<number> | 是 | 否 | 相应请求权限的结果。0表示授权成功,-1表示失败。 | diff --git a/zh-cn/application-dev/reference/apis/js-apis-processrunninginfo.md b/zh-cn/application-dev/reference/apis/js-apis-processrunninginfo.md new file mode 100644 index 0000000000000000000000000000000000000000..d22b2afe122785331b69c77ac474f52512dda061 --- /dev/null +++ b/zh-cn/application-dev/reference/apis/js-apis-processrunninginfo.md @@ -0,0 +1,36 @@ +# ProcessRunningInfo + +- [使用说明](#使用说明) +- [属性](#属性) + + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** +> 本模块首批接口从API 8开始支持。 + + +进程运行信息。 + + +## 使用说明 + + +通过appManager来获取。 + + + +``` +import appManager from '@ohos.application.appManager'; +appManager.getProcessRunningInfos((error,data) => { + console.log("getProcessRunningInfos error: " + error.code + " data: " + JSON.stringify(data)); +}); +``` + + +## 属性 + + | 名称 | 参数类型 | 可读 | 可写 | 说明 | +| -------- | -------- | -------- | -------- | -------- | +| pid | number | 是 | 否 | 进程ID。 | +| uid | number | 是 | 否 | 用户ID。 | +| processName | string | 是 | 否 | 进程名称。 | +| bundleNames | Array<string> | 是 | 否 | 进程中所有运行的包名称。 | diff --git a/zh-cn/application-dev/reference/apis/js-apis-uripermissionmanager.md b/zh-cn/application-dev/reference/apis/js-apis-uripermissionmanager.md new file mode 100644 index 0000000000000000000000000000000000000000..34cc33c69437954800242c7cbeff198bccd1da5f --- /dev/null +++ b/zh-cn/application-dev/reference/apis/js-apis-uripermissionmanager.md @@ -0,0 +1,76 @@ +# UriPermissionManager + +- [导入模块](#导入模块) +- [verifyUriPermission](#verifyUriPermission) +- [verifyUriPermission](#verifyUriPermission) + + +> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:** +> 本模块首批接口从API 9开始支持。 + + +Uri权限管理。 + + +## 导入模块 + + +``` +import UriPermissionManager from '@@ohos.application.UriPermissionManager'; +``` + + +## verifyUriPermission + +verifyUriPermission(uri: string, flag: wantConstant.Flags, accessTokenId: number, callback: AsyncCallback<number>): void + +检验某个应用是否对指定的uri有flag指定的权限。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | uri | string | 是 | 指向文件的uri,例如fileshare:///com.samples.filesharetest.FileShare/person/10。 | + | flag | wantConstant.Flags | 是 | uri的读权限或写权限。 | + | accessTokenId | number | 是 | 每个应用的唯一标识ID,开发者通过包管理接口自行获取。 | + | callback | AsyncCallback<number> | 是 | callback形式返回启动结果,返回0表示有权限,返回-1表示无权限。 | + +- 示例: + + ``` + let uri = "fileshare:///com.samples.filesharetest.FileShare/person/10" + UriPermissionManager.verifyUriPermission(uri, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION, accessTokenId, (result) => { + console.log("result.code = " + result.code) + }) // accessTokenId开发者通过包管理接口自行获取 + ``` + + +## verifyUriPermission + +verifyUriPermission(uri: string, flag: wantConstant.Flags, accessTokenId: number): Promise<number> + +检验某个应用是否对指定的uri有flag指定的权限。 + +- 参数: + | 参数名 | 类型 | 必填 | 说明 | + | -------- | -------- | -------- | -------- | + | uri | string | 是 | 指向文件的uri,例如fileshare:///com.samples.filesharetest.FileShare/person/10。 | + | flag | wantConstant.Flags | 是 | uri的读权限或写权限。 | + | accessTokenId | number | 是 | 每个应用的唯一标识ID,开发者通过包管理接口自行获取。 | + +- 返回值: + | 类型 | 说明 | + | -------- | -------- | + | Promise<number> | 返回0表示有权限,返回-1表示无权限。 | + +- 示例: + + ``` + let uri = "fileshare:///com.samples.filesharetest.FileShare/person/10" + UriPermissionManager.verifyUriPermission(uri, wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION, accessTokenId) + .then((data) => { + console.log('Verification succeeded.' + data) + }).catch((error) => { + console.log('Verification failed.'); + }) + ``` +