valalarmManager:AlarmManager=context.getSystemService<AlarmManager>()!!when{// If permission is granted, proceed with scheduling exact alarms.alarmManager.canScheduleExactAlarms()->{alarmManager.setExact(...)}else->{// Ask users to go to exact alarm page in system settings.startActivity(Intent(ACTION_REQUEST_SCHEDULE_EXACT_ALARM))}}
overridefunonResume(){…if(alarmManager.canScheduleExactAlarms()){// Set exact alarms.alarmManager.setExact(...)}else{// Permission not yet approved. Display user notice and revert to a fallback // approach.alarmManager.setWindow(...)}}
ทำงานอย่างราบรื่นเมื่อถูกปฏิเสธสิทธิ์
ผู้ใช้บางรายจะปฏิเสธที่จะให้สิทธิ์ดังกล่าว ในสถานการณ์นี้ เราขอแนะนำ
แอปเพื่อลดระดับประสบการณ์การใช้งานอย่างแนบเนียนและยังคงพยายามนำเสนอสิ่งที่ดีที่สุด
ประสบการณ์ของผู้ใช้ทางเลือกที่เป็นไปได้โดยการระบุ Use Case ของผู้ใช้
[null,null,["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],null,["# Schedule exact alarms are denied by default\n\nExact alarms are meant for user-intentioned notifications or actions that need\nto happen at a precise time.\n\n[`SCHEDULE_EXACT_ALARM`](/reference/android/Manifest.permission#SCHEDULE_EXACT_ALARM), the permission introduced in Android 12 for apps to\nschedule exact alarms, is **no longer being pre-granted to most newly installed\napps targeting Android 13 and higher** (will be set to denied by default). If\nthe user transfers app data to a device running Android 14 through a\nbackup-and-restore operation, the permission will still be denied. If an\nexisting app already has this permission, it'll be pre-granted when the device\nupgrades to Android 14.\n\nThe [`SCHEDULE_EXACT_ALARM`](/reference/android/Manifest.permission#SCHEDULE_EXACT_ALARM) permission is required to initiate exact alarms\nvia the following APIs or a `SecurityException` will be thrown:\n\n- [`setExact()`](/reference/android/app/AlarmManager#setExact(int,%20long,%20android.app.PendingIntent))\n- [`setExactAndAllowWhileIdle()`](/reference/android/app/AlarmManager#setExactAndAllowWhileIdle(int,%20long,%20android.app.PendingIntent))\n- [`setAlarmClock()`](/reference/android/app/AlarmManager#setAlarmClock(android.app.AlarmManager.AlarmClockInfo,%20android.app.PendingIntent))\n\n| **Note:** If the exact alarm is set using an [`OnAlarmListener`](/reference/android/app/AlarmManager.OnAlarmListener) object, like in the [`setExact`](/reference/android/app/AlarmManager#setExact(int,%20long,%20java.lang.String,%20android.app.AlarmManager.OnAlarmListener,%20android.os.Handler)) API, the `SCHEDULE_EXACT_ALARM` permission isn't required.\n\nExisting best-practices for the [`SCHEDULE_EXACT_ALARM`](/reference/android/Manifest.permission#SCHEDULE_EXACT_ALARM) permission still\napply, including the following:\n\n- Check the permission with [`canScheduleExactAlarms()`](/reference/android/app/AlarmManager#canScheduleExactAlarms()) before scheduling exact alarms.\n- Set up your app to listen and properly react to the foreground broadcast [`AlarmManager.ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED`](/reference/android/app/AlarmManager#ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED), which the system sends when the user grants the permission.\n\nAffected apps\n-------------\n\nIf a device is running Android 14 or higher, this change will affect a newly\ninstalled app that has the following characteristics:\n\n- Targets Android 13 (API level 33) or higher.\n- Declares the [`SCHEDULE_EXACT_ALARM`](/reference/android/Manifest.permission#SCHEDULE_EXACT_ALARM) permission in the manifest.\n- Doesn't fall under an [exemption](#exemptions) or [pre-grant](#pre-grant) scenario.\n- **Isn't** a calendar or alarm clock app.\n\nCalendar and alarm clock apps should declare USE_EXACT_ALARM\n------------------------------------------------------------\n\n[Calendar or alarm clock apps](https://support.google.com/googleplay/android-developer/answer/12253906#exact_alarm_preview) need to send calendar reminders, wake-up\nalarms, or alerts when the app is no longer running. These apps can request the\n[`USE_EXACT_ALARM`](/reference/android/Manifest.permission#USE_EXACT_ALARM) normal permission. The `USE_EXACT_ALARM` permission will\nbe granted on install, and apps holding this permission will be able to schedule\nexact alarms just like apps with the [`SCHEDULE_EXACT_ALARM`](/reference/android/Manifest.permission#SCHEDULE_EXACT_ALARM) permission.\n| **Note:** Apps will not be able to publish a version of their app with this permission in the manifest unless they qualify based on the [policy language](https://support.google.com/googleplay/android-developer/answer/12253906#exact_alarm_preview).\n\nUse cases that might not require exact alarms\n---------------------------------------------\n\nBecause the [`SCHEDULE_EXACT_ALARM`](/reference/android/Manifest.permission#SCHEDULE_EXACT_ALARM) permission is now denied by default and\nthe permission grant process requires extra steps from users, developers are\nstrongly encouraged to evaluate their use cases and determine if exact alarms\nstill make sense for their use cases.\n\nThe following list shows common workflows that may not require an exact alarm:\n\nScheduling repeated work during the lifetime of your app\n: The [`set()`](/reference/android/app/AlarmManager#set(int,%20long,%20android.app.PendingIntent)) method is useful if the task needs to keep real-time\n constraints in mind, such as going off at 2:00 PM tomorrow or in 30 minutes.\n Otherwise, it's recommended to use the [`postAtTime()`](/reference/android/os/Handler#postAtTime(java.lang.Runnable,%20long)) or\n [`postDelayed()`](/reference/android/os/Handler#postDelayed(java.lang.Runnable,%20long)) methods instead.\n\nScheduled background work, such as updating your app and uploading logs\n: `WorkManager` provides a way to [schedule timing-sensitive periodic work](/topic/libraries/architecture/workmanager/how-to/define-work#flexible_run_intervals).\n You can provide a repeat interval and flexInterval (15 minutes minimum) to\n define granular runtime for the work.\n\nNeed alarm to go off at an approximate time while system is in idle state\n: Use an inexact alarm. Specifically, call [`setAndAllowWhileIdle()`](/reference/android/app/AlarmManager#setAndAllowWhileIdle(int,%20long,%20android.app.PendingIntent)).\n\nUser-specified action that should happen after a specific time\n: Use an inexact alarm. Specifically, call [`set()`](/reference/android/app/AlarmManager#set(int,%20long,%20android.app.PendingIntent)).\n\nUser-specified action that can happen within a time window\n: Use an inexact alarm. Specifically, call [`setWindow()`](/reference/android/app/AlarmManager#setWindow(int,%20long,%20long,%20android.app.PendingIntent)). Note that the\n smallest allowed window length is 10 minutes.\n\nMigration steps to continue using exact alarms\n----------------------------------------------\n\nAt a minimum, apps must check to see if they have the permission before\nscheduling exact alarms. If apps don't have the permission, they must request it\nfrom the user by invoking an intent.\n\nThis is the same as the standard workflow for [requesting a special\npermission](/training/permissions/requesting-special):\n\n1. Apps should call [`AlarmManager.canScheduleExactAlarms()`](/reference/android/app/AlarmManager#canScheduleExactAlarms()) to confirm that it has the appropriate permission.\n2. If the app doesn't have the permission, invoke an intent that includes the\n [`ACTION_REQUEST_SCHEDULE_EXACT_ALARM`](/reference/android/provider/Settings#ACTION_REQUEST_SCHEDULE_EXACT_ALARM), along with the app's package\n name, to ask users to grant the permission.\n\n [Check the user's decision](/reference/android/app/AlarmManager#set(int,%20long,%20android.app.PendingIntent)) in the [`onResume()`](/guide/components/activities/activity-lifecycle#onresume) method of your\n app.\n3. Listen for the\n [`AlarmManager.ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED`](/reference/android/app/AlarmManager#ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED)\n broadcasts that are sent if the user grants the permission.\n\n4. If the user granted the permission to your app, your app can set exact\n alarms. If the user denied the permission instead, [gracefully degrade your\n app experience](#gracefully-degrade) so that it provides functionality to\n the user without the information that's protected by that permission.\n\nThe following code snippet demonstrates how to check for the\n`SCHEDULE_EXACT_ALARM` permission: \n\n val alarmManager: AlarmManager = context.getSystemService\u003cAlarmManager\u003e()!!\n when {\n // If permission is granted, proceed with scheduling exact alarms.\n alarmManager.canScheduleExactAlarms() -\u003e {\n alarmManager.setExact(...)\n }\n else -\u003e {\n // Ask users to go to exact alarm page in system settings.\n startActivity(Intent(ACTION_REQUEST_SCHEDULE_EXACT_ALARM))\n }\n }\n\nSample code to check the permission and handle the user's decisions in\n`onResume()`: \n\n override fun onResume() {\n ... \n if (alarmManager.canScheduleExactAlarms()) {\n // Set exact alarms.\n alarmManager.setExact(...)\n }\n else {\n // Permission not yet approved. Display user notice and revert to a fallback \n // approach.\n alarmManager.setWindow(...)\n }\n }\n\n### Gracefully degrade on permission denial\n\nSome users will refuse to grant the permission. In this scenario, we recommend\napps to gracefully degrade the experience and still strive to provide the best\npossible fallback user experience by identifying their [use cases](#use-cases).\n\nExemptions\n----------\n\nThe following types of apps are always allowed to call the [`setExact()`](/reference/android/app/AlarmManager#setExact(int,%20long,%20android.app.PendingIntent)) or\n[`setExactAndAllowWhileIdle()`](/reference/android/app/AlarmManager#setExactAndAllowWhileIdle(int,%20long,%20android.app.PendingIntent)) methods:\n\n- Apps signed with the platform certificate.\n- Privileged apps.\n- Apps that are on the power allowlist (if your app fits the requirements, you can [request this](/training/monitoring-device-state/doze-standby#exemption-cases) using the `ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS` intent action).\n\nPre-grants\n----------\n\n- Role holders of `SYSTEM_WELLBEING` will be pre-granted [`SCHEDULE_EXACT_ALARM`](/reference/android/Manifest.permission#SCHEDULE_EXACT_ALARM).\n\nTesting guidelines\n------------------\n\nTo test this change, disable the **Alarms \\& reminders** permission for your app\nfrom the **Special app access** page in system settings (**Settings \\\u003e Apps \\\u003e\nSpecial app access \\\u003e Alarms \\& reminders**) and observe the behavior of your app."]]