如何正确触发ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS意图?

如文档中所述:

“拥有REQUEST_IGNORE_BATTERY_OPTIMIZATIONS权限的应用程序可以触发系统对话框,让用户直接将应用程序添加到白名单,而无需进行设置。应用程序会触发ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS Intent来触发对话框。”

有人能告诉我解雇这个意图的正确方法吗?

Solutions Collecting From Web of "如何正确触发ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS意图?"

Intent intent = new Intent(); String packageName = context.getPackageName(); PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); if (pm.isIgnoringBatteryOptimizations(packageName)) intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS); else { intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); intent.setData(Uri.parse("package:" + packageName)); } context.startActivity(intent); 

有关更多信息,请参阅此答案 。

为了避免从Google Play商店暂停,最好将用户带到电池优化设置,以便手动将应用程序添加到白名单

 Intent myIntent = new Intent(); myIntent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS); startActivity(myIntent); 

它也不需要

  

清单文件中的权限

  Intent myIntent = new Intent(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { myIntent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); myIntent.setData(Uri.parse("package:" + getActivity().getPackageName())); } startActivity(myIntent);