HarmonyOS 鸿蒙Next 获取日历失败

发布于 1周前 作者 h691938207 来自 鸿蒙OS

HarmonyOS 鸿蒙Next 获取日历失败

获取日历失败,下面是申请权限及获取日历的函数

代码:

深色代码主题
复制
  async getCalendarInfo() {
// 日历的读写权限
const permissions: Permissions[] = [‘ohos.permission.READ_CALENDAR’, ‘ohos.permission.WRITE_CALENDAR’];
let atManager = abilityAccessCtrl.createAtManager();
atManager.requestPermissionsFromUser(mContext, permissions).then((result: PermissionRequestResult) => {
console.log(===test=== get Permission success, result: <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(result)}</span>);
calendarMgr = calendarManager.getCalendarManager(mContext);
}).catch((error: BusinessError) => {
console.error(===test=== get Permission error, error: <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(error)}</span>);
})

calendarMgr?.<span class="hljs-title function_">getCalendar</span>(<span class="hljs-function">(<span class="hljs-params">err: BusinessError, data: calendarManager.Calendar</span>) =&gt;</span> {
  <span class="hljs-keyword">if</span> (err) {
    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">error</span>(<span class="hljs-string">`===test=== Failed to get calendar. Code: <span class="hljs-subst">${err.code}</span>, message: <span class="hljs-subst">${err.message}</span>`</span>);
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">info</span>(<span class="hljs-string">`===test=== Succeeded to get calendar data -&gt; <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(data)}</span>`</span>);
  }
});

}

日志:

深色代码主题
复制
12-18 16:49:01.722   25244-25244   C0391E/com.hua…st/AceGesture  com.huawei.todolist   I     [(100000:100000:scope)] Click try accept
12-18 16:49:01.722   25244-25244   C03951/com.huaw…InputKeyFlow  com.huawei.todolist   I     [(100000:100000:scope)] Click accepted, tag: Button
12-18 16:49:01.724   25244-25244   C05A01/com.hua…estPermission  com.huawei.todolist   I     [ParseRequestPermissionFromUser:645]AsyncContext.uiAbilityFlag is: 1.
12-18 16:49:01.724   25244-25244   C05A02/com.hua…st/CommonNapi  com.huawei.todolist   I     [ParseStringArray:139]Array size is 2
12-18 16:49:01.728   25244-25244   C01201/com.huaw…EventHandler  com.huawei.todolist   I     [(EventHandler:66)]log suppressed cnt 12
12-18 16:49:01.728   25244-25244   C01201/com.huaw…EventHandler  com.huawei.todolist   I     Create eventHandler 14_56686941938222
12-18 16:49:01.729   25244-25244   C03919/com.hua…InputTracking  com.huawei.todolist   I     [(100000:100000:scope)] Consumed new event id=13983 in ace_container, lastEventInfo: id:13982
12-18 16:49:01.729   25244-25494   C05A01/com.hua…estPermission  com.huawei.todolist   I     [IsDynamicRequest:446]TokenID: 537066762.
12-18 16:49:01.730   25244-25494   C057C6/com.hua…BinderInvoker  com.huawei.todolist   I     BinderInvoker 92: created invoker 38707584
12-18 16:49:01.732   25244-25494   C05A01/com.hua…list/ATMProxy  com.huawei.todolist   I     [GetSelfPermissionsState:368]Result from server (status=0).
12-18 16:49:01.732   25244-25494   C05A01/com.hua…estPermission  com.huawei.todolist   I     [IsDynamicRequest:456]Permission: ohos.permission.READ_CALENDAR: state: 2
12-18 16:49:01.732   25244-25494   C05A01/com.hua…estPermission  com.huawei.todolist   I     [IsDynamicRequest:456]Permission: ohos.permission.WRITE_CALENDAR: state: 2
12-18 16:49:01.732   25244-25494   C05A01/com.hua…estPermission  com.huawei.todolist   I     [RequestPermissionsFromUserExecute:693]It does not need to request permission
12-18 16:49:01.732   25244-25244   A03D00/com.hua…odolist/JSAPP  com.huawei.todolist   I     ===test=== get Permission success, result: {“permissions”:[“ohos.permission.READ_CALENDAR”,“ohos.permission.WRITE_CALENDAR”],“authResults”:[2,2],“dialogShownResults”:[false,false]}
12-18 16:49:01.732   25244-25244   A0050A/com.hua…t/CalendarApi  com.huawei.todolist   I     [calendar_manager_napi.cpp(GetCalendarManager:210)]napi GetCalendarManager called
12-18 16:49:01.732   25244-25244   A0050A/com.hua…t/CalendarApi  com.huawei.todolist   I     [napi_env.cpp(Init:57)]CalendarEnvNapi Init.
12-18 16:49:01.733   25244-25244   A0050A/com.hua…t/CalendarApi  com.huawei.todolist   I     [napi_env.cpp(Init:68)]verify access result=-1
12-18 16:49:01.733   25244-25244   C057C1/com.hua…ocessSkeleton  com.huawei.todolist   W     AttachObject 179: attach 34236992 desc:*.ISystemAbilityStatusChange assign
12-18 16:49:01.734   25244-25244   C01651/com.hua…ist/DataShare  com.huawei.todolist   I     [Connect()-ams_mgr_proxy.cpp:70]: connect start, uri = *****/calendardata
12-18 16:49:01.734   25244-25244   C057C2/com.hua…PCObjectProxy  com.huawei.todolist   I     SendRequest 137: handle:12 desc:.AbilityManager refcnt:6 29724224
12-18 16:49:01.735   25244-25269   C01651/com.hua…ist/DataShare  com.huawei.todolist   I     [operator()()-data_share_manager_impl.cpp:137]: RecoverObs start
12-18 16:49:01.736   25244-25244   C01651/com.hua…ist/DataShare  com.huawei.todolist   I     [ConnectDataShareExtAbility()-datashare_connection.cpp:180]: connect ability, uri = ******/calendardata. ret = 2097208
12-18 16:49:01.736   25244-25244   C01651/com.hua…ist/DataShare  com.huawei.todolist   E     [CreateExtHelper()-datashare_helper.cpp:198]: connect failed
12-18 16:49:01.736   25244-25244   A0050A/com.hua…t/CalendarApi  com.huawei.todolist   I     [napi_env.cpp(Init:75)]dataShareHelper low create result=0
12-18 16:49:01.736   25244-25244   A0050A/com.hua…t/CalendarApi  com.huawei.todolist   D     [napi_queue.cpp(~ContextBase:20)]no memory leak after callback or promise[resolved/rejected]
12-18 16:49:01.736   25244-25244   C01201/com.huaw…EventHandler  com.huawei.todolist   I     ~EventHandler enter 14_56686941938222
12-18 16:49:01.822   25244-25269   C01406/com.hua…list/OHOS::RS  com.huawei.todolist   I     RSUIDirector::ProcessMessages messageId:2, cmdCount:1, instanceId:100000
12-18 16:49:01.823   25244-25244   C01406/com.hua…list/OHOS::RS  com.huawei.todolist   I     RSUIDirector::PostTask messageId:2, cmdCount:1, instanceId:100000
12-18 16:49:06.730   25244-25496   C01719/com.hua…todolist/ffrt  com.huawei.todolist   W     155:~WorkerThread:65 WorkerThread enter destruction
12-18 16:49:06.730   25244-25496   C057C6/com.hua…BinderInvoker  com.huawei.todolist   I     ~BinderInvoker 97: destroyed invoker 95904128
12-18 16:49:06.738   25244-25499   C01719/com.hua…todolist/ffrt  com.huawei.todolist   W     159:~WorkerThread:65 WorkerThread enter destruction


更多关于HarmonyOS 鸿蒙Next 获取日历失败的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

5 回复

获取权限成功后的操作是异步的,你是直接在这部分代码后执行的操作吗

更多关于HarmonyOS 鸿蒙Next 获取日历失败的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


贴在帖子里的代码是这样的,我也尝试过在onWindowStageCreate函数中申请权限,结果是一样的,

申请权限的日志打印是state=2,但是仍然返回的成功 12-18 16:49:01.732 25244-25494 C05A01/com.hua…estPermission com.huawei.todolist I [IsDynamicRequest:456]Permission: ohos.permission.READ_CALENDAR: state: 2 12-18 16:49:01.732 25244-25494 C05A01/com.hua…estPermission com.huawei.todolist I [IsDynamicRequest:456]Permission: ohos.permission.WRITE_CALENDAR: state: 2

module.json5文件requestPermissions这个权限配置了吗?ohos.permission.READ_CALENDAR ohos.permission.WRITE_CALENDAR 本地测试了下是可以的

module.json5文件中没有配置权限,配置后正常了。 感谢!!!

在HarmonyOS鸿蒙Next系统中,如果遇到获取日历失败的问题,通常可能是由以下几个原因导致的:

  1. 权限未授予:确保应用已正确申请并获得了访问日历的权限。检查应用权限设置,确保日历权限已开启。

  2. API调用错误:检查获取日历数据的API调用是否正确。核对API文档,确保使用的接口、参数和方法符合规范。

  3. 系统错误或Bug:系统本身可能存在错误或Bug,导致日历数据无法获取。可以尝试重启设备或更新系统至最新版本。

  4. 数据同步问题:如果日历数据依赖于云服务同步,检查网络连接和云服务状态,确保数据同步正常。

  5. 应用兼容性问题:部分应用可能不完全兼容HarmonyOS系统,导致特定功能(如获取日历)失效。确认应用版本与系统版本的兼容性。

  6. 第三方应用冲突:检查是否有其他第三方应用影响了日历的正常访问,尝试卸载或禁用可能冲突的应用。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html 。客服人员将提供更专业的帮助和解决方案。

回到顶部