valalarmManager=getSystemService<AlarmManager>()!!when{// if permission is granted, proceed with scheduling exact alarms…alarmManager.canScheduleExactAlarms()->{alarmManager.setExact(...)}else->{// ask users to grant the permission in the corresponding settings pagestartActivity(Intent(ACTION_REQUEST_SCHEDULE_EXACT_ALARM))}}
在 onResume() 中检查权限和处理用户决定的示例代码:
overridefunonResume(){// ...if(alarmManager.canScheduleExactAlarms()){// proceed with the action (setting exact alarms)alarmManager.setExact(...)}else{// permission not yet approved. Display user notice and gracefully degradeyourappexperience.alarmManager.setWindow(...)}}
[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Request special permissions\n\nA *special permission* guards access to system resources that are particularly\nsensitive or not directly related to user privacy. These permissions are\ndifferent than [install-time\npermissions](/guide/topics/permissions/overview#install-time) and [runtime\npermissions](/guide/topics/permissions/overview#runtime). \n**Figure 1.** The **Special app access** screen in system settings.\n\nSome examples of special permissions include:\n\n- Scheduling exact alarms.\n- Displaying and drawing over other apps.\n- Accessing all storage data.\n\nApps that declare a special permission are shown in the **Special app access**\npage in system settings (figure 1). To grant a special permission to the app, a\nuser must navigate to this page: **Settings \\\u003e Apps \\\u003e Special app access**.\n| **Note:** Special permissions should be only used in specific use cases, and there may be policy implications to adding them in your app,\n\nWorkflow\n--------\n\nTo request a special permission, do the following:\n\n1. In your app's manifest file, [declare the special\n permissions](/training/permissions/declaring) that your app might need to request.\n2. Design your app's UX so that specific actions in your app are associated with specific special permissions. Let users know which actions might require them to grant permission for your app to access private user data.\n3. [Wait for the user](/training/permissions/requesting#principles) to invoke the task or action in your app that requires access to specific private user data. At that time, your app can request the special permission that's required for accessing that data.\n4. Check whether the user has already granted the special permission that your app requires. To do so, use each permission's [custom checking\n function](#check-method). If granted, your app can access the private user data. If not, continue to the next step. Note: You must check whether you have the permission every time you perform an operation that requires that permission.\n5. [Present a rationale](#explain) to the user in a UI element that clearly explains what data your app is trying to access and what benefits the app can provide to the user if they grant the special permission. In addition, since your app sends users to system settings to grant the permission, also include brief instructions that explain how users can grant the permission there. The rationale UI should provide a clear option for the user to opt-out of granting the permission. After the user acknowledges the rationale, continue to the next step.\n6. [Request the special permission](#request) that your app requires to access the private user data. This likely involves an intent to the corresponding page in system settings where the user can grant the permission. Unlike [runtime permissions](/guide/topics/permissions/overview#runtime), there is no popup permission dialog.\n7. Check the user's response -- whether they chose to grant or deny the special permission -- in the `onResume()` method.\n8. If the user granted the permission to your app, you can access the private user data. If the user denied the permission instead, [gracefully degrade\n your app experience](/training/permissions/requesting#handle-denial) so that it provides functionality to the user without the information that's protected by that permission.\n\n**Figure 2.** Workflow for declaring and requesting special permissions on Android.\n\nRequest special permissions\n---------------------------\n\nUnlike [runtime permissions](/guide/topics/permissions/overview#runtime), the\nuser must grant special permissions from the **Special App Access** page in\nsystem settings. Apps can send users there using an intent, which pauses the app\nand launches the corresponding settings page for a given special permission.\nAfter the user returns to the app, the app can check if the permission has been\ngranted in the `onResume()` function.\n\nThe following sample code shows how to request the\n[`SCHEDULE_EXACT_ALARMS`](/reference/android/Manifest.permission#SCHEDULE_EXACT_ALARM)\nspecial permission from users: \n\n val alarmManager = 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 grant the permission in the corresponding settings page\n startActivity(Intent(ACTION_REQUEST_SCHEDULE_EXACT_ALARM))\n }\n }\n\nSample code to check the permission and handle user decisions in `onResume()`: \n\n override fun onResume() {\n // ...\n\n if (alarmManager.canScheduleExactAlarms()) {\n // proceed with the action (setting exact alarms)\n alarmManager.setExact(...)\n }\n else {\n // permission not yet approved. Display user notice and gracefully degrade\n your app experience.\n alarmManager.setWindow(...)\n }\n }\n\nBest practices and tips\n-----------------------\n\nThe following sections provide some best practices and considerations when\nrequesting special permissions.\n\n### Each permission has its own check method\n\nSpecial permissions operate differently than [runtime\npermissions](/training/permissions/requesting#request-permission). Instead,\nrefer to the [permissions API reference\npage](/reference/android/Manifest.permission) and use the custom access check\nfunctions for each special permission. Examples include\n[`AlarmManager#canScheduleExactAlarms()`](/reference/android/app/AlarmManager#canScheduleExactAlarms())\nfor the\n[`SCHEDULE_EXACT_ALARMS`](/reference/android/Manifest.permission#SCHEDULE_EXACT_ALARM)\npermission and\n[`Environment#isExternalStorageManager()`](/reference/android/os/Environment#isExternalStorageManager())\nfor the\n[`MANAGE_EXTERNAL_STORAGE`](/reference/android/Manifest.permission#MANAGE_EXTERNAL_STORAGE)\npermission.\n\n### Request in-context\n\nSimilar to runtime permissions, apps should request special permissions\nin-context when the user requests a specific action that requires the\npermission. For example, wait to request the `SCHEDULE_EXACT_ALARMS` permission\nuntil the user schedules an email to be sent at a specific time.\n\n### Explain the request\n\nProvide a rationale before redirecting to system settings. Since users leave the\napp temporarily to grant special permissions, show an in-app UI before you\nlaunch the intent to the **Special App Access** page in system settings. This UI\nshould clearly explain why the app needs the permission and how the user should\ngrant it on the settings page."]]