harmony(鸿蒙)默认应用管理

2022-08-09 浏览 (1095)

默认应用管理

本模块提供查询默认应用的能力,支持查询当前应用是否是默认应用。

说明:

本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

import defaultAppMgr from '@ohos.bundle.defaultAppManager';

defaultAppMgr.ApplicationType

应用类型

系统能力: SystemCapability.BundleManager.BundleFramework.DefaultAppManager

名称类型说明
BROWSERstringWeb Browser默认浏览器。
IMAGEstringImage Gallery默认图片查看器。
AUDIOstringAudio Player默认音频播放器。
VIDEOstringVideo Player默认视频播放器。
PDFstringPDF Viewer默认PDF文档查看器。
WORDstringWord Viewer默认WORD文档查看器。
EXCELstringExcel Viewer默认EXCEL文档查看器。
PPTstringPPT Viewer默认PPT文档查看器。

defaultAppMgr.isDefaultApplication

isDefaultApplication(type: string): Promise<boolean>

以异步方法根据系统已定义的应用类型判断当前应用是否是该应用类型的默认应用,使用Promise形式返回结果。

系统能力: SystemCapability.BundleManager.BundleFramework.DefaultAppManager

参数:

名称类型必填描述
typestring要查询的应用类型,取ApplicationType中的值。

返回值:

类型说明
Promise<boolean>Promise形式返回当前应用是否是默认应用,true表示是默认应用,false表示不是默认应用。

示例:

defaultAppMgr.isDefaultApplication(defaultAppMgr.ApplicationType.BROWSER)
.then((data) => {
    console.info('Operation successful. IsDefaultApplication ? ' + JSON.stringify(data));
}).catch((error) => {
    console.error('Operation failed. Cause: ' + JSON.stringify(error));
});

defaultAppMgr.isDefaultApplication

isDefaultApplication(type: string, callback: AsyncCallback<boolean>): void

以异步方法根据系统已定义的应用类型判断当前应用是否是该应用类型的默认应用,使用callback形式返回结果。

系统能力: SystemCapability.BundleManager.BundleFramework.DefaultAppManager

参数:

名称类型必填描述
typestring要查询的应用类型,取ApplicationType中的值。
callbackAsyncCallback<boolean>程序启动作为入参的回调函数,返回当前应用是否是默认应用,true表示是默认应用,false表示不是默认应用。

示例:

defaultAppMgr.isDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, (err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful. IsDefaultApplication ? ' + JSON.stringify(data));
 });

defaultAppMgr.getDefaultApplication

getDefaultApplication(type: string, userId?: number): Promise<BundleInfo>

以异步方法根据系统已定义的应用类型或者符合媒体类型格式(type/subtype)的文件类型获取默认应用信息,使用Promise形式返回结果。

需要权限: ohos.permission.GET_DEFAULT_APPLICATION

系统能力: SystemCapability.BundleManager.BundleFramework.DefaultAppManager

系统API: 此接口为系统接口,三方应用不支持调用

参数:

名称类型必填描述
typestring要查询的应用类型,取ApplicationType中的值,或者符合媒体类型格式的文件类型。
userIdnumber用户ID。默认值:调用方所在用户。

返回值:

类型说明
Promise<BundleInfo>Promise形式返回默认应用包信息。

错误码:

错误码ID错误码信息
17700004The specified user id is not found.
17700023The specified default app does not exist.
17700025The specified type is invalid.

示例:

defaultAppMgr.getDefaultApplication(defaultAppMgr.ApplicationType.BROWSER)
.then((data) => {
    console.info('Operation successful. bundleInfo: ' + JSON.stringify(data));
})
.catch((error) => {
    console.error('Operation failed. Cause: ' + JSON.stringify(error));
});

defaultAppMgr.getDefaultApplication("image/png")
.then((data) => {
    console.info('Operation successful. bundleInfo: ' + JSON.stringify(data));
})
.catch((error) => {
    console.error('Operation failed. Cause: ' + JSON.stringify(error));
});

defaultAppMgr.getDefaultApplication

getDefaultApplication(type: string, userId: number, callback: AsyncCallback<BundleInfo>) : void

以异步方法根据系统已定义的应用类型或者符合媒体类型格式(type/subtype)的文件类型获取默认应用信息,使用callback形式返回结果。

需要权限: ohos.permission.GET_DEFAULT_APPLICATION

系统能力: SystemCapability.BundleManager.BundleFramework.DefaultAppManager

系统API: 此接口为系统接口,三方应用不支持调用

参数:

名称类型必填描述
typestring要查询的应用类型,取ApplicationType中的值,或者符合媒体类型格式的文件类型。
userIdnumber用户ID。
callbackAsyncCallback<BundleInfo>程序启动作为入参的回调函数,返回包信息。

错误码:

错误码ID错误码信息
17700004The specified user id is not found.
17700023The specified default app does not exist.
17700025The specified type is invalid.

示例:

defaultAppMgr.getDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, 100, (err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful. bundleInfo:' + JSON.stringify(data));
});

defaultAppMgr.getDefaultApplication("image/png", 100, (err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful. bundleInfo:' + JSON.stringify(data));
});

defaultAppMgr.getDefaultApplication

getDefaultApplication(type: string, callback: AsyncCallback<BundleInfo>) : void

以异步方法根据系统已定义的应用类型或者符合媒体类型格式(type/subtype)的文件类型获取默认应用信息,使用callback形式返回结果。

需要权限: ohos.permission.GET_DEFAULT_APPLICATION

系统能力: SystemCapability.BundleManager.BundleFramework.DefaultAppManager

系统API: 此接口为系统接口,三方应用不支持调用

参数:

名称类型必填描述
typestring要查询的应用类型,取ApplicationType中的值,或者符合媒体类型格式的文件类型。
callbackAsyncCallback<BundleInfo>程序启动作为入参的回调函数,返回包信息。

错误码:

错误码ID错误码信息
17700004The specified user id is not found.
17700023The specified default app does not exist.
17700025The specified type is invalid.

示例:

defaultAppMgr.getDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, (err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful. bundleInfo:' + JSON.stringify(data));
});

defaultAppMgr.getDefaultApplication("image/png", (err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful. bundleInfo:' + JSON.stringify(data));
});

defaultAppMgr.setDefaultApplication

setDefaultApplication(type: string, elementName: ElementName, userId?: number): Promise<void>

以异步方法根据系统已定义的应用类型或者符合媒体类型格式(type/subtype)的文件类型设置默认应用,使用Promise形式返回结果。

需要权限: ohos.permission.SET_DEFAULT_APPLICATION

系统能力: SystemCapability.BundleManager.BundleFramework.DefaultAppManager.defaultAppManager

系统API: 此接口为系统接口,三方应用不支持调用

参数:

名称类型必填描述
typestring要设置的应用类型,取ApplicationType中的值,或者符合媒体类型格式的文件类型。
elementNameElementName要设置为默认应用的组件信息。
userIdnumber用户ID。默认值:调用方所在用户。

错误码:

错误码ID错误码信息
17700004The specified user id is not found.
17700025The specified type is invalid.
17700028The specified ability and type does not match.

示例:

defaultAppMgr.setDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, {
    bundleName: "com.test.app",
    moduleName: "module01",
    abilityName: "MainAbility"
})
.then((data) => {
    console.info('Operation successful.');
})
.catch((error) => {
    console.error('Operation failed. Cause: ' + JSON.stringify(error));
});

defaultAppMgr.setDefaultApplication("image/png", {
    bundleName: "com.test.app",
    moduleName: "module01",
    abilityName: "MainAbility"
})
.then((data) => {
    console.info('Operation successful.');
})
.catch((error) => {
    console.error('Operation failed. Cause: ' + JSON.stringify(error));
});

defaultAppMgr.setDefaultApplication

setDefaultApplication(type: string, elementName: ElementName, userId: number, callback: AsyncCallback<void>) : void;

以异步方法根据系统已定义的应用类型或者符合媒体类型格式(type/subtype)的文件类型设置默认应用,使用callback形式返回结果。

需要权限: ohos.permission.SET_DEFAULT_APPLICATION

系统能力: SystemCapability.BundleManager.BundleFramework.DefaultAppManager.defaultAppManager

系统API: 此接口为系统接口,三方应用不支持调用

参数:

名称类型必填描述
typestring要设置的应用类型,取ApplicationType中的值,或者符合媒体类型格式的文件类型。
elementNameElementName要设置为默认应用的组件信息。
userIdnumber用户ID。
callbackAsyncCallback<void>程序启动作为入参的回调函数。

错误码:

错误码ID错误码信息
17700004The specified user id is not found.
17700025The specified type is invalid.
17700028The specified ability and type does not match.

示例:

defaultAppMgr.setDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, {
    bundleName: "com.test.app",
    moduleName: "module01",
    abilityName: "MainAbility"
}, 100, (err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful.');
 });

defaultAppMgr.setDefaultApplication("image/png", {
    bundleName: "com.test.app",
    moduleName: "module01",
    abilityName: "MainAbility"
}, 100, (err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful.');
 });

defaultAppMgr.setDefaultApplication

setDefaultApplication(type: string, elementName: ElementName, callback: AsyncCallback<void>) : void;

以异步方法根据系统已定义的应用类型或者符合媒体类型格式(type/subtype)的文件类型设置默认应用,使用callback形式返回结果。

需要权限: ohos.permission.SET_DEFAULT_APPLICATION

系统能力: SystemCapability.BundleManager.BundleFramework.DefaultAppManager.defaultAppManager

系统API: 此接口为系统接口,三方应用不支持调用

参数:

名称类型必填描述
typestring要设置的应用类型,取ApplicationType中的值,或者符合媒体类型格式的文件类型。
elementNameElementName要设置为默认应用的组件信息。
callbackAsyncCallback<void>程序启动作为入参的回调函数。

错误码:

错误码ID错误码信息
17700004The specified user id is not found.
17700025The specified type is invalid.
17700028The specified ability and type does not match.

示例:

defaultAppMgr.setDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, {
    bundleName: "com.test.app",
    moduleName: "module01",
    abilityName: "MainAbility"
}, (err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful.');
 });

defaultAppMgr.setDefaultApplication("image/png", {
    bundleName: "com.test.app",
    moduleName: "module01",
    abilityName: "MainAbility"
}, (err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful.');
 });

defaultAppMgr.resetDefaultApplication

resetDefaultApplication(type: string, userId?: number): Promise<void>

以异步方法根据系统已定义的应用类型或者符合媒体类型格式(type/subtype)的文件类型重置默认应用,使用Promise形式返回结果。

需要权限: ohos.permission.SET_DEFAULT_APPLICATION

系统能力: SystemCapability.BundleManager.BundleFramework.DefaultAppManager.defaultAppManager

系统API: 此接口为系统接口,三方应用不支持调用

参数:

名称类型必填描述
typestring要重置的应用类型,取ApplicationType中的值,或者符合媒体类型格式的文件类型。
userIdnumber用户ID。默认值:调用方所在用户。

错误码:

错误码ID错误码信息
17700004The specified user id is not found.
17700025The specified type is invalid.

示例:

defaultAppMgr.resetDefaultApplication(defaultAppMgr.ApplicationType.BROWSER)
.then((data) => {
    console.info('Operation successful.');
})
.catch((error) => {
    console.error('Operation failed. Cause: ' + JSON.stringify(error));
});

defaultAppMgr.resetDefaultApplication("image/png")
.then((data) => {
    console.info('Operation successful.');
})
.catch((error) => {
    console.error('Operation failed. Cause: ' + JSON.stringify(error));
});

defaultAppMgr.resetDefaultApplication

resetDefaultApplication(type: string, userId: number, callback: AsyncCallback<void>) : void;

以异步方法根据系统已定义的应用类型或者符合媒体类型格式(type/subtype)的文件类型重置默认应用,使用callback形式返回结果。

需要权限: ohos.permission.SET_DEFAULT_APPLICATION

系统能力: SystemCapability.BundleManager.BundleFramework.DefaultAppManager.defaultAppManager

系统API: 此接口为系统接口,三方应用不支持调用

参数:

名称类型必填描述
typestring要重置的应用类型,取ApplicationType中的值,或者符合媒体类型格式的文件类型。
userIdnumber用户ID。
callbackAsyncCallback<void>程序启动作为入参的回调函数。

错误码:

错误码ID错误码信息
17700004The specified user id is not found.
17700025The specified type is invalid.

示例:

defaultAppMgr.resetDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, 100, (err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful.');
});

defaultAppMgr.resetDefaultApplication("image/png", 100, (err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful.');
});

defaultAppMgr.resetDefaultApplication

resetDefaultApplication(type: string, callback: AsyncCallback<void>) : void;

以异步方法根据系统已定义的应用类型或者符合媒体类型格式(type/subtype)的文件类型重置默认应用,使用callback形式返回结果。

需要权限: ohos.permission.SET_DEFAULT_APPLICATION

系统能力: SystemCapability.BundleManager.BundleFramework.DefaultAppManager.defaultAppManager

系统API: 此接口为系统接口,三方应用不支持调用

参数:

名称类型必填描述
typestring要重置的应用类型,取ApplicationType中的值,或者符合媒体类型格式的文件类型。
callbackAsyncCallback<void>程序启动作为入参的回调函数。

错误码:

错误码ID错误码信息
17700004The specified user id is not found.
17700025The specified type is invalid.

示例:

defaultAppMgr.resetDefaultApplication(defaultAppMgr.ApplicationType.BROWSER, (err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful.');
});

defaultAppMgr.resetDefaultApplication("image/png", (err, data) => {
    if (err) {
        console.error('Operation failed. Cause: ' + JSON.stringify(err));
        return;
    }
    console.info('Operation successful.');
});

你可能感兴趣的鸿蒙文章

harmony(鸿蒙)接口

harmony(鸿蒙)开发说明

harmony(鸿蒙)BundleStatusCallback

harmony(鸿蒙)innerBundleManager模块(JS端SDK接口)

harmony(鸿蒙)distributedBundle模块(JS端SDK接口)

harmony(鸿蒙)Bundle模块(JS端SDK接口)

harmony(鸿蒙)Context模块

harmony(鸿蒙)DataUriUtils模块

harmony(鸿蒙)EnterpriseAdminExtensionAbility

harmony(鸿蒙)延迟任务调度回调

  • 所属分类: 后端技术
  • 本文标签: 软件 鸿蒙
  • 版权声明: 本文链接 https://seaxiang.com/blog/59cb01846a2e4f099b340997e1278f67