可以切换警报和提醒许可

发布于 2025-02-08 01:15:31 字数 586 浏览 3 评论 0原文

我有设置警报的应用程序。我使用此代码向用户询问许可:

Intent intent = new Intent(
"android.settings.REQUEST_SCHEDULE_EXACT_ALARM",
 Uri.parse("package:"+ getPackageName())
);
 someActivityResultLauncher.launch(intent);

在清单中,我添加了权限:

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

当我单击启动意图的按钮时,我可以看到:

“在此处输入映像说明”

但是我无法通过许可进行切换,我不don' T知道为什么。

I have app which set alarms. I use this code to ask user for permission:

Intent intent = new Intent(
"android.settings.REQUEST_SCHEDULE_EXACT_ALARM",
 Uri.parse("package:"+ getPackageName())
);
 someActivityResultLauncher.launch(intent);

In manifest I added permission:

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

When I click the button which launch the intent I can see:

enter image description here

But I can't toggle button with permission and I don't know why.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天的雪花 2025-02-15 01:15:31

它将返回iSgranted = true当请求androidmanifest.xml时。

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

And actually, the intent is Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM:

ActivityResultLauncher<Intent> startActivityForResult = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(), (ActivityResult result) -> {
            if (result.getResultCode() == RESULT_OK) {}
        });

@RequiresApi(api = Build.VERSION_CODES.S_V2)
void hasPermission(@NonNull Context context) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S_V2) {

        // it depends on the manifest permission, just alike the toggle does.
        if (! alarmManager.canScheduleExactAlarms()) {

        }

        // one can only disable the permission, when the manifest requested it.
        startActivityForResult.launch(new Intent(
                Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM,
                Uri.parse("package:" + getPackageName())
        ));
    }
}

Listen for broadcast action

而且还有许可 use_exact_alarm针对API 33开始。

参见行为更改:

It will return isGranted = true when the permission is being requested the AndroidManifest.xml.

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

And actually, the intent is Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM:

ActivityResultLauncher<Intent> startActivityForResult = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(), (ActivityResult result) -> {
            if (result.getResultCode() == RESULT_OK) {}
        });

@RequiresApi(api = Build.VERSION_CODES.S_V2)
void hasPermission(@NonNull Context context) {
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S_V2) {

        // it depends on the manifest permission, just alike the toggle does.
        if (! alarmManager.canScheduleExactAlarms()) {

        }

        // one can only disable the permission, when the manifest requested it.
        startActivityForResult.launch(new Intent(
                Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM,
                Uri.parse("package:" + getPackageName())
        ));
    }
}

Listen for broadcast action AlarmManager.ACTION_SCHEDULE_EXACT_ALARM_PERMISSION_STATE_CHANGED.

And there's also permission USE_EXACT_ALARM, when targeting API 33 onwards.

See behavioral changes: exact alarm permission.

鹤仙姿 2025-02-15 01:15:31
// Button click for check permission 
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S_V2) {
                        hasPermission(AlarmActivity.this);
                    }
// 2 Number code

 @RequiresApi(api = Build.VERSION_CODES.S_V2)
    void hasPermission(@NonNull Context context) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S_V2) {

            // it depends on the manifest permission, just alike the toggle does.
            if (!alarmManager.canScheduleExactAlarms()) {
                startActivityForResult.launch(new Intent(
                        Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM,
                        Uri.parse("package:" + getPackageName())
                ));
            }

            // one can only disable the permission, when the manifest requested it.

        }
    } 
// 3 Number code 

ActivityResultLauncher<Intent> startActivityForResult = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(), (ActivityResult result) -> {
                if (result.getResultCode() == RESULT_OK) {

                }
            });
// Button click for check permission 
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S_V2) {
                        hasPermission(AlarmActivity.this);
                    }
// 2 Number code

 @RequiresApi(api = Build.VERSION_CODES.S_V2)
    void hasPermission(@NonNull Context context) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S_V2) {

            // it depends on the manifest permission, just alike the toggle does.
            if (!alarmManager.canScheduleExactAlarms()) {
                startActivityForResult.launch(new Intent(
                        Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM,
                        Uri.parse("package:" + getPackageName())
                ));
            }

            // one can only disable the permission, when the manifest requested it.

        }
    } 
// 3 Number code 

ActivityResultLauncher<Intent> startActivityForResult = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(), (ActivityResult result) -> {
                if (result.getResultCode() == RESULT_OK) {

                }
            });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文