REQUEST_IGNORE_BATTERY_OPTIMIZATIONS 如何正确地做 [英] REQUEST_IGNORE_BATTERY_OPTIMIZATIONS how to do it right

查看:60
本文介绍了REQUEST_IGNORE_BATTERY_OPTIMIZATIONS 如何正确地做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在前台模式下有 IntentService 任务,但在 Android M+ 中,任务在打盹模式下停止.如果应用程序使用意图将自己设置在白名单中,我读到谷歌被禁止.但是,如果我使用许可并检查 GRANT 或 DENIED,我会得到授予的结果,但没有任何反应.我在白名单中没有看到我的应用程序.如何在不禁止的情况下将应用程序添加到白名单?(我在 AndroidManifest.xml 中添加了权限)

I have IntentService task in foreground mode, but in Android M+ the task stops in Doze mode. I read Google banned if the app uses intent to set themselves in whitelist. But if I use permission and check GRANT or DENIED, I get the granted result, but nothing happen. I don't see my app in whitelist. How can I add the app in whitelist without banned? (I added permission in AndroidManifest.xml)

if(Build.VERSION.SDK_INT>=23){
    int permissionCheck= ContextCompat
                    .checkSelfPermission(this, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);

    if(permissionCheck == PackageManager.PERMISSION_DENIED){

        //Should we show an explanation
        if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)){
            //Show an explanation
            final String message = "";
             Snackbar.make(coordinatorLayoutView,message,Snackbar.LENGTH_LONG)
                     .setAction("GRANT", new View.OnClickListener() {
                         @Override
                         public void onClick(View v) {
                             ActivityCompat.requestPermissions(MainActivity.this, new String[]{ Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS }, PERMISSION_REQUEST_CODE);
                         }
                     })
                     .show();

                }else{
                    //No explanation need,we can request the permission
                    ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS }, PERMISSION_REQUEST_CODE);
                }
            }
        }

推荐答案

REQUEST_IGNORE_BATTERY_OPTIMIZATIONS 不是 dangerous 权限.您不需要或不想要任何该代码.引用 REQUEST_IGNORE_BATTERY_OPTIMIZATIONS 的文档:

REQUEST_IGNORE_BATTERY_OPTIMIZATIONS is not a dangerous permission. You do not need, or want, any of that code. Quoting the documentation for REQUEST_IGNORE_BATTERY_OPTIMIZATIONS:

应用程序必须持有权限才能使用 ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS.这是一个正常的权限:请求它的应用程序将始终被授予该权限,而无需用户批准或查看它.

Permission an application must hold in order to use ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS. This is a normal permission: an app requesting it will always be granted the permission, without the user needing to approve or see it.

所以,删除所有代码.

我在白名单中没有看到我的应用.

I don't see my app in whitelist.

那是因为用户显然没有将您添加到白名单中.

That is because the user did not add you to the whitelist, apparently.

请求 REQUEST_IGNORE_BATTERY_OPTIMIZATIONS 授予您从安全角度出发使用 一个ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS 意图.请务必将您的应用程序包包含为 Uri:

Requesting REQUEST_IGNORE_BATTERY_OPTIMIZATIONS grants you the authority, from a security standpoint, to start an activity with an ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Intent. Be sure to include your app's package as the Uri:

startActivity(new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Uri.parse("package:"+getPackageName())));

用户将被带到一个屏幕,在那里他们可以表明他们愿意暂停您应用上的部分 Doze 模式效果.

The user will be taken to a screen where they can indicate that they are willing to suspend portions of Doze mode effects on your app.

如何在不禁止的情况下将应用添加到白名单?

How can I add the app in whitelist without banned?

如果您不想被禁止,请不要做任何这些.在您的应用中添加一些内容,使用 一个 ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS代码> <代码>意图.这会将用户引导至应用程序的整体列表,用户可以在其中切换哪些应用程序在白名单中,哪些不在白名单中.这不需要任何许可.

If you do not want to be banned, do not do any of this. Have something in your app that starts an activity with an ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS Intent. This leads the user to the overall list of apps, where the user can toggle which ones are and are not on the whitelist. This does not require any permission.

在清单中请求 REQUEST_IGNORE_BATTERY_OPTIMIZATIONS 的行为是 什么可能让你被禁止.

The act of requesting REQUEST_IGNORE_BATTERY_OPTIMIZATIONS in the manifest is what may get you banned.

这篇关于REQUEST_IGNORE_BATTERY_OPTIMIZATIONS 如何正确地做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆