Behavior changes adapted to Android 12-----Precise alarm clock permissions

       To encourage apps to conserve system resources, apps targeting Android 12 and higher that set precise alarms must have access to the Alarms & Reminders feature, which appears in the Special App Access screen in System Settings. To obtain this special application access, request the SCHEDULE_EXACT_ALARM permission in the manifest.

For the developer website, please refer to:

Behavior changes: Apps targeting Android 12 | Android Developers

       To put it simply, if the methods setAlarmClock(), setExact(), and setExactAndAllowWhileIdle() are used in the code, a precise alarm clock will be set. In the Android12 environment, when calling these methods, if the SCHEDULE_EXACT_ALARM permission is not turned on, the application will crash (test before doing this. If these methods are called after the permission is turned off, if the application does not crash, there should be no need to respond). Therefore, when calling these methods, add permission checks.

solution:

(1). Apply for permission

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />

(2). Jump to the authorization screen

The processing flow of Google Pixel: Check the permissions when opening the application. If the SCHEDULE_EXACT_ALARM permission is not turned on, use toast to prompt the user to open the authorization interface. If the user does not turn on the set alarm clock, it will not work (my suggestion is Play the dialog, and if the user refuses, just exit the application directly to save trouble later)

Uri uri = Uri.parse("package:"+this.getPackageName());
Intent i = new Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM,uri);
startActivity(i,200);

SCHEDULE_EXACT_ALARM permission authorization screen

(3). Determine whether you have permission

alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
boolean  hasPermission = alarmManager.canScheduleExactAlarms();//true:有权限,false:没有权限
if(hasPermission){
	setAlarmClock();//设置精准闹钟的方法
	setExact();
	setExactAndAllowWhileIdle();
}else{
	//如果没检查到权限,应该做出相应的处理
	//(1).什么都不做,写一条log。如果权限关闭闹钟将无法使用
	//(2).设置不精准的闹钟
	//(3).使用dialog、toast等提示用户打开权限(具体项目,具体设计,不建议在每次设置精准闹钟时候都对用户进行提示,这样的用户体验感不好)
}

 important point:

1. If the user sets the alarm when the SCHEDULE_EXACT_ALARM permission is turned on and turns off the alarm before the alarm clock expires, the alarm will not sound. If the user finds that the alarm does not sound when the time is up, the user can turn on the SCHEDULE_EXACT_ALARM permission and the alarm can sound ( If the alarm clock expires, the alarm clock will be missed)

 Corresponding method: Set up a broadcast receiver (both static and dynamic) and receive "AlarmManager.ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED".
 Note here: the broadcast can only be received when the precise alarm clock permission is turned on; it cannot be received when it is turned off. CheckPermission every time the method is used.
 If you cannot receive the broadcast, run the following command in the terminal to enable the behavior update: (the simulator must also run)
 adb shell am compat enable REQUIRE_EXACT_ALARM_PERMISSION [packageName]

Normally, the SCHEDULE_EXACT_ALARM permission is turned on by default. Users should not find this and turn it off, let alone perform such non-human operations, but as developers we should consider it more comprehensively. I plan to wait until Android 12 is adapted, and then I will find fault with various mobile phone manufacturers. I have already found trouble with Google Pixel once, and Google has not dealt with the situation I mentioned here (as of November).

2. App info and System Setting both have SCHEDULE_EXACT_ALARM permissions. Although their authorization interfaces look the same, they are different interfaces. This will cause a problem. If the permission is turned off in App info, two authorization interfaces may be jumped when jumping to the authorization interface.
 Corresponding method: StartActivity when clicking the jump button, so that the authorization interface of App info (AlarmsAndRemindersAppActivity) can be overwritten.

public void createAlertDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage(R.string.extra_alarm_permission_descript_title);
        builder.setPositiveButton(R.string.extra_alarm_permission_turn_on, new DialogInterface.OnClickListener() {
            Uri uri = Uri.parse("package:" + "com.android.deskclock");
            Intent intent = new Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM, uri);
            public void onClick(DialogInterface dialog, int which) {
                startActivity(intent);
            }
        });
        builder.setNegativeButton(R.string.extra_alarm_permission_ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        mExtraAlarmDialog = builder.create();
        mExtraAlarmDialog.setCancelable(false);
        mExtraAlarmDialog.show();
    }

Guess you like

Origin blog.csdn.net/m0_50408097/article/details/122000782