目录

使用restore时出现801和1300004错误码时如何处理

问题现象

使用平板和PC设备运行官网restore()示例代码,分别出现801错误码和1300004错误码。

使用平板出现错误码801:

使用PC设备出现错误码1300004:

背景知识

问题定位

import { UIAbility } from '@kit.AbilityKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { window } from '@kit.ArkUI';


export default class EntryAbility extends UIAbility {
  onWindowStageCreate(windowStage: window.WindowStage): void {
    try {
      let windowClass = windowStage.getMainWindowSync();
      // 调用minimize,使主窗最小化
      setTimeout(() => {
        windowClass.minimize();
        console.info('窗口最小化');
      }, 3000);
      // 设置延时函数延时5秒钟后对主窗进行恢复。
      setTimeout(() => {
        console.info('定时器运行');
        // 调用restore()函数对主窗进行恢复。
        let promise = windowClass.restore();
        promise.then(() => {
          console.info('Succeeded in restoring the window.');
        }).catch((err: BusinessError) => {
          console.error(`Failed to restore the window. Cause code: ${err.code}, message: ${err.message}`);
        });
      }, 6000);
    } catch (exception) {
      console.error(`Failed to restore the window. Cause code: ${exception.code}, message: ${exception.message}`);
    }
  }
};

分析结论

官网明确规定restore()在2in1设备或平板设备的自由多窗模式下使用,出现不支持此API和没有操作权限的报错,说明存在不支持的设备。

修改建议

可以查看下module.json5文件下deviceTypes下的有没有phone这个字段。

"deviceTypes": [
  "phone",
  "tablet"
],

有的话把phone这个字段移除即可。