DevEco Studio和系统升级后,EntryAbility.ets文件创建悬浮窗失败?

在DevEco Studio和系统升级前,在EntryAbility.ets文件可以创建悬浮窗,升级到最新的之后创建失败。
代码如下:

let floatWindowClass: window.Window | null = null; 
// 1.创建悬浮窗 
const config: window.Configuration = { 
  name: "floatWindow", windowType: window.WindowType.TYPE_FLOAT, ctx: this.context 
}; 
window.createWindow(config, (err: BusinessError, data) => { 
  let errCode: number = err.code; 
  if (errCode) { 
    console.error('floatWindowClass Failed to create the floatWindow. Cause: ' + JSON.stringify(err)); 
    return; 
  } 
  console.info('floatWindowClass Succeeded in creating the floatWindow. Data: ' + JSON.stringify(data)); 
  { 
    let errCode: number = err.code; 
    if (errCode) { 
      console.error('floatWindowClass Failed to create the floatWindow. Cause: ' + JSON.stringify(err)); 
      return; 
    } 
    console.info('floatWindowClass Succeeded in creating the floatWindow. Data: ' + JSON.stringify(data)); 
    floatWindowClass = data; 
    AppStorage.setOrCreate('floatWindowClass', floatWindowClass); 
    // 2.悬浮窗窗口创建成功后,设置悬浮窗的位置、大小及相关属性等。 
    floatWindowClass.moveWindowTo(0, 604, (err: BusinessError) => { 
      let errCode: number = err.code; 
      if (errCode) { 
        console.error('floatWindowClass Failed to move the window. Cause:' + JSON.stringify(err)); 
        return; 
      } 
      console.info('floatWindowClass Succeeded in moving the window.'); 
    }); 
    // 3.为悬浮窗加载对应的目标页面。 
    floatWindowClass.setUIContent("pages/view/AudioComponent", (err: BusinessError) => { 
      let errCode: number = err.code; 
      if (errCode) { 
        console.error('floatWindowClass Failed to load the content. Cause:' + JSON.stringify(err)); 
        return; 
      } 
      console.info('floatWindowClass Succeeded in loading the content.'); 
    }); 
 
    floatWindowClass.on('windowEvent', (data) => { 
      EmitterUtils.sendEvent(EmitterEventId.AUDIO_WINDOW_TYPE, data) 
    }); 
  })
阅读 743
avatarAI BotBETA

问题解答

在DevEco Studio和HarmonyOS系统升级后,创建悬浮窗失败的问题可能由多种原因引起。以下是一些可能的解决步骤和检查点:

  1. 权限检查

    • 确保你的应用已经在config.json文件中声明了创建悬浮窗所需的权限。对于HarmonyOS,通常需要ohos.permission.DISPLAY_POPUP_WINDOW权限。
  2. API变更

    • 检查最新的API文档,看是否有关于window.createWindowmoveWindowTosetUIContent等API的变更。有时候,API的参数或行为会在新版本中发生变化。
  3. 上下文(Context)问题

    • 确保传递给createWindowctx(上下文)是有效的。在HarmonyOS中,错误的上下文可能导致创建窗口失败。
  4. 错误处理

    • 你的代码中错误处理部分被重复了两次,这可能是一个复制粘贴错误。请确保只保留一套错误处理逻辑。
  5. 日志分析

    • 仔细查看控制台输出的错误日志,了解BusinessError的具体内容。这通常会提供关于为什么创建窗口失败的详细原因。
  6. 系统限制

    • 检查是否有新的系统限制或策略阻止了悬浮窗的创建。例如,某些系统更新可能增加了对悬浮窗创建的额外限制。
  7. 示例代码和文档

    • 查阅最新的HarmonyOS官方文档和示例代码,看看是否有关于如何在新版本中创建悬浮窗的最新指导。
  8. 清理和重建项目

    • 有时候,简单的清理和重建项目可以解决由于编译缓存或配置错误导致的问题。
  9. 联系技术支持

    • 如果以上步骤都不能解决问题,考虑联系HarmonyOS的技术支持团队或访问开发者论坛寻求帮助。

示例代码修正

以下是修正后的代码片段,移除了重复的错误处理部分:

let floatWindowClass: window.Window | null = null;
// 1. 创建悬浮窗
const config: window.Configuration = {
  name: "floatWindow",
  windowType: window.WindowType.TYPE_FLOAT,
  ctx: this.context
};
window.createWindow(config, (err: BusinessError, data) => {
  if (err) {
    console.error('Failed to create the floatWindow. Cause: ' + JSON.stringify(err));
    return;
  }
  console.info('Succeeded in creating the floatWindow. Data: ' + JSON.stringify(data));
  floatWindowClass = data;
  AppStorage.setOrCreate('floatWindowClass', floatWindowClass);
  // 2. 悬浮窗窗口创建成功后,设置悬浮窗的位置、大小及相关属性等。
  floatWindowClass.moveWindowTo(0, 604, (err: BusinessError) => {
    if (err) {
      console.error('Failed to move the window. Cause:' + JSON.stringify(err));
      return;
    }
    console.info('Succeeded in moving the window.');
  });
  // 3. 为悬浮窗加载对应的目标页面。
  floatWindowClass.setUIContent("pages/view/AudioComponent", (err: BusinessError) => {
    if (err) {
      console.error('Failed to load the content. Cause:' + JSON.stringify(err));
      return;
    }
    console.info('Succeeded in loading the content.');
  });

  floatWindowClass.on('windowEvent', (data) => {
    EmitterUtils.sendEvent(EmitterEventId.AUDIO_WINDOW_TYPE, data);
  });
});

确保你的代码符合最新的API标准和最佳实践。

1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题