harmony(鸿蒙)DefaultAppManager

2022-08-09 浏览 (775)

DefaultAppManager

The DefaultAppManager module provides APIs to query whether the current application is the default application of a specific type.

NOTE

The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

Modules to Import

import defaultAppMgr from '@ohos.bundle.defaultAppManager'

defaultAppMgr.ApplicationType

Enumerates the application types.

System capability: SystemCapability.BundleManager.BundleFramework

NameTypeDescription
BROWSERstringDefault browser.
IMAGEstringDefault image viewer.
AUDIOstringDefault audio player.
VIDEOstringDefault video player.
PDFstringDefault PDF reader.
WORDstringDefault Word viewer.
EXCELstringDefault Excel viewer.
PPTstringDefault PowerPoint viewer.

defaultAppMgr.isDefaultApplication

isDefaultApplication(type: string): Promise<boolean>

Checks whether this application is the default application of a system-defined application type. This API uses a promise to return the result.

System capability: SystemCapability.BundleManager.BundleFramework

Parameters

NameTypeMandatoryDescription
typestringYesType of the target application. It must be set to a value defined by ApplicationType.

Return value

TypeDescription
Promise<boolean>Promise used to return the result. If the application is the default application, true is returned; otherwise, false is returned.

Example

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

Checks whether this application is the default application of a system-defined application type. This API uses an asynchronous callback to return the result.

System capability: SystemCapability.BundleManager.BundleFramework

Parameters

NameTypeMandatoryDescription
typestringYesType of the target application. It must be set to a value defined by ApplicationType.
callbackAsyncCallback<boolean>YesCallback used to return the result. If the application is the default application, true is returned; otherwise, false is returned.

Example

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>

Obtains the default application based on a system-defined application type or a file type that complies with the media type format (either specified by type or subtype). This API uses a promise to return the result.

Required permissions: ohos.permission.GET_DEFAULT_APPLICATION

System capability: SystemCapability.BundleManager.BundleFramework

System API: This is a system API and cannot be called by third-party applications.

Parameters

NameTypeMandatoryDescription
typestringYesType of the target application. It must be set to a value defined by ApplicationType or a file type that complies with the media type format.
userIdnumberNoUser ID. The default value is the user ID of the caller.

Return value

TypeDescription
Promise<BundleInfo>Promise used to return the default application.

Example

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

Obtains the default application of a user based on a system-defined application type or a file type that complies with the media type format (either specified by type or subtype). This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.GET_DEFAULT_APPLICATION

System capability: SystemCapability.BundleManager.BundleFramework

System API: This is a system API and cannot be called by third-party applications.

Parameters

NameTypeMandatoryDescription
typestringYesType of the target application. It must be set to a value defined by ApplicationType or a file type that complies with the media type format.
userIdnumberYesUser ID.
callbackAsyncCallback<BundleInfo>YesCallback used to return the default application.

Example

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

Obtains the default application based on a system-defined application type or a file type that complies with the media type format (either specified by type or subtype). This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.GET_DEFAULT_APPLICATION

System capability: SystemCapability.BundleManager.BundleFramework

System API: This is a system API and cannot be called by third-party applications.

Parameters

NameTypeMandatoryDescription
typestringYesType of the target application. It must be set to a value defined by ApplicationType or a file type that complies with the media type format.
callbackAsyncCallback<BundleInfo>YesCallback used to return the default application.

Example

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>

Sets the default application based on a system-defined application type or a file type that complies with the media type format (either specified by type or subtype). This API uses a promise to return the result.

Required permissions: ohos.permission.SET_DEFAULT_APPLICATION

System capability: SystemCapability.BundleManager.BundleFramework

System API: This is a system API and cannot be called by third-party applications.

Parameters

NameTypeMandatoryDescription
typestringYesType of the target application. It must be set to a value defined by ApplicationType or a file type that complies with the media type format.
elementNameElementNameYesInformation about the element to be set as the default application.
userIdnumberNoUser ID. The default value is the user ID of the caller.

Example

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;

Sets the default application for a user based on a system-defined application type or a file type that complies with the media type format (either specified by type or subtype). This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.SET_DEFAULT_APPLICATION

System capability: SystemCapability.BundleManager.BundleFramework

System API: This is a system API and cannot be called by third-party applications.

Parameters

NameTypeMandatoryDescription
typestringYesType of the target application. It must be set to a value defined by ApplicationType or a file type that complies with the media type format.
elementNameElementNameYesInformation about the element to be set as the default application.
userIdnumberYesUser ID.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

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;

Sets the default application based on a system-defined application type or a file type that complies with the media type format (either specified by type or subtype). This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.SET_DEFAULT_APPLICATION

System capability: SystemCapability.BundleManager.BundleFramework

System API: This is a system API and cannot be called by third-party applications.

Parameters

NameTypeMandatoryDescription
typestringYesType of the target application. It must be set to a value defined by ApplicationType or a file type that complies with the media type format.
elementNameElementNameYesInformation about the element to be set as the default application.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

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>

Resets the default application based on a system-defined application type or a file type that complies with the media type format (either specified by type or subtype). This API uses a promise to return the result.

Required permissions: ohos.permission.SET_DEFAULT_APPLICATION

System capability: SystemCapability.BundleManager.BundleFramework

System API: This is a system API and cannot be called by third-party applications.

Parameters

NameTypeMandatoryDescription
typestringYesType of the target application. It must be set to a value defined by ApplicationType or a file type that complies with the media type format.
userIdnumberNoUser ID. The default value is the user ID of the caller.

Example

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;

Resets the default application for a user based on a system-defined application type or a file type that complies with the media type format (either specified by type or subtype). This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.SET_DEFAULT_APPLICATION

System capability: SystemCapability.BundleManager.BundleFramework

System API: This is a system API and cannot be called by third-party applications.

Parameters

NameTypeMandatoryDescription
typestringYesType of the target application. It must be set to a value defined by ApplicationType or a file type that complies with the media type format.
userIdnumberYesUser ID.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

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;

Resets the default application based on a system-defined application type or a file type that complies with the media type format (either specified by type or subtype). This API uses an asynchronous callback to return the result.

Required permissions: ohos.permission.SET_DEFAULT_APPLICATION

System capability: SystemCapability.BundleManager.BundleFramework

System API: This is a system API and cannot be called by third-party applications.

Parameters

NameTypeMandatoryDescription
typestringYesType of the target application. It must be set to a value defined by ApplicationType or a file type that complies with the media type format.
callbackAsyncCallback<void>YesCallback used to return the result.

Example

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(鸿蒙)APIs

harmony(鸿蒙)API Reference Document Description

harmony(鸿蒙)BundleStatusCallback

harmony(鸿蒙)innerBundleManager

harmony(鸿蒙)distributedBundle

harmony(鸿蒙)Bundle

harmony(鸿蒙)Context

harmony(鸿蒙)DataUriUtils

harmony(鸿蒙)EnterpriseAdminExtensionAbility

harmony(鸿蒙)Work Scheduler Callbacks

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