如何在打ze模式下将清单白名单应用Android 6.0

这个问题与将于本月底最终发布的Android 6.0 Preview 3有关。


我正在Nexus 5“ hammerhead”上通过Google在预览版3中测试Android 6.0中的某些功能。


新功能是“打mode模式”-类似于深度睡眠模式(当网络被禁用且电话处于睡眠状态时),只有SMS,电话或高优先级GCM消息才能将其唤醒。但是像WhatsApp一样-在打ze模式下,它会在2小时或更长时间后接收消息,具体取决于计时器。但是,有一个名为“白名单”的“未优化”应用程序列表,您可以在其中手动添加应用程序。


好的,我想找到一种方法,以编程方式将我的应用程序添加到电池设置中设备中存在的“白名单应用程序列表”中,而无需用户交互。


尝试使用反射进入它,我发现:


在android.os.IDeviceIdleController中,有一个方法:


公共抽象无效addPowerSaveWhitelistApp(字符串packageNameOfApp)


但这是一个接口...因此,我们无法创建接口的实例。


尚无有关此接口,方法或任何继承树的文档。


也许您有一些想法,我应该在哪里寻找以编程方式添加我的应用程序的可能性?


还有一种方法


公共抽象布尔isPowerSaveWhitelistApp(字符串packageName)


我认为应该可以以某种方式访问哪个?检查该应用程序是否存在于白名单中,并且可能最终希望ASK用户将其添加到白名单中。


所以我的问题是,你们当中有人尝试过做出更好的结果吗?因为我被卡住了,我认为这是一个死胡同。


有关更多信息:https : //newcircle.com/s/post/1739/2015/06/12/diving-into-android-m-doze


qq_花开花谢_0
浏览 795回答 3
3回答

达令说

添加权限<uses-permission&nbsp;android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>请求将您的应用列入白名单&nbsp;if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String packageName = getPackageName();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!pm.isIgnoringBatteryOptimizations(packageName)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.setData(Uri.parse("package:" + packageName));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }

慕森卡

如果没有在Android M预览版3上进行用户交互,则无法禁用电池优化(=打ze模式的白名单应用程序)。可以通过以下方式与用户交互来完成:Intent intent = new Intent();String packageName = context.getPackageName();PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);if (pm.isIgnoringBatteryOptimizations(packageName))&nbsp; &nbsp; intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);else {&nbsp; &nbsp; intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);&nbsp; &nbsp; intent.setData(Uri.parse("package:" + packageName));}context.startActivity(intent);

30秒到达战场

我认为该帮助班应该满足您的所有需求。要使用它来请求操作系统将您的应用列入白名单,您可以使用prepareIntentForWhiteListingOfBatteryOptimization。如果您得到null,则表示您不需要它或无法使用它。您可以使用其他功能查询您所处状态的更好状态。public class PowerSaverHelper {&nbsp; &nbsp; public enum PowerSaveState {&nbsp; &nbsp; &nbsp; &nbsp; ON, OFF, ERROR_GETTING_STATE, IRRELEVANT_OLD_ANDROID_API&nbsp; &nbsp; }&nbsp; &nbsp; public enum WhiteListedInBatteryOptimizations {&nbsp; &nbsp; &nbsp; &nbsp; WHITE_LISTED, NOT_WHITE_LISTED, ERROR_GETTING_STATE, UNKNOWN_TOO_OLD_ANDROID_API_FOR_CHECKING, IRRELEVANT_OLD_ANDROID_API&nbsp; &nbsp; }&nbsp; &nbsp; public enum DozeState {&nbsp; &nbsp; &nbsp; &nbsp; NORMAL_INTERACTIVE, DOZE_TURNED_ON_IDLE, NORMAL_NON_INTERACTIVE, ERROR_GETTING_STATE, IRRELEVANT_OLD_ANDROID_API, UNKNOWN_TOO_OLD_ANDROID_API_FOR_CHECKING&nbsp; &nbsp; }&nbsp; &nbsp; @NonNull&nbsp; &nbsp; public static DozeState getDozeState(@NonNull Context context) {&nbsp; &nbsp; &nbsp; &nbsp; if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return DozeState.IRRELEVANT_OLD_ANDROID_API;&nbsp; &nbsp; &nbsp; &nbsp; if (VERSION.SDK_INT < VERSION_CODES.M) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return DozeState.UNKNOWN_TOO_OLD_ANDROID_API_FOR_CHECKING;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; final PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);&nbsp; &nbsp; &nbsp; &nbsp; if (pm == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return DozeState.ERROR_GETTING_STATE;&nbsp; &nbsp; &nbsp; &nbsp; return pm.isDeviceIdleMode() ? DozeState.DOZE_TURNED_ON_IDLE : pm.isInteractive() ? DozeState.NORMAL_INTERACTIVE : DozeState.NORMAL_NON_INTERACTIVE;&nbsp; &nbsp; }&nbsp; &nbsp; @NonNull&nbsp; &nbsp; public static PowerSaveState getPowerSaveState(@NonNull Context context) {&nbsp; &nbsp; &nbsp; &nbsp; if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return PowerSaveState.IRRELEVANT_OLD_ANDROID_API;&nbsp; &nbsp; &nbsp; &nbsp; final PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);&nbsp; &nbsp; &nbsp; &nbsp; if (pm == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return PowerSaveState.ERROR_GETTING_STATE;&nbsp; &nbsp; &nbsp; &nbsp; return pm.isPowerSaveMode() ? PowerSaveState.ON : PowerSaveState.OFF;&nbsp; &nbsp; }&nbsp; &nbsp; @NonNull&nbsp; &nbsp; public static WhiteListedInBatteryOptimizations getIfAppIsWhiteListedFromBatteryOptimizations(@NonNull Context context, @NonNull String packageName) {&nbsp; &nbsp; &nbsp; &nbsp; if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return WhiteListedInBatteryOptimizations.IRRELEVANT_OLD_ANDROID_API;&nbsp; &nbsp; &nbsp; &nbsp; if (VERSION.SDK_INT < VERSION_CODES.M)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return WhiteListedInBatteryOptimizations.UNKNOWN_TOO_OLD_ANDROID_API_FOR_CHECKING;&nbsp; &nbsp; &nbsp; &nbsp; final PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);&nbsp; &nbsp; &nbsp; &nbsp; if (pm == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return WhiteListedInBatteryOptimizations.ERROR_GETTING_STATE;&nbsp; &nbsp; &nbsp; &nbsp; return pm.isIgnoringBatteryOptimizations(packageName) ? WhiteListedInBatteryOptimizations.WHITE_LISTED : WhiteListedInBatteryOptimizations.NOT_WHITE_LISTED;&nbsp; &nbsp; }&nbsp; &nbsp; @TargetApi(VERSION_CODES.M)&nbsp; &nbsp; @RequiresPermission(permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)&nbsp; &nbsp; @Nullable&nbsp; &nbsp; public static Intent prepareIntentForWhiteListingOfBatteryOptimization(@NonNull Context context, @NonNull String packageName, boolean alsoWhenWhiteListed) {&nbsp; &nbsp; &nbsp; &nbsp; if (VERSION.SDK_INT < VERSION_CODES.LOLLIPOP)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; if (ContextCompat.checkSelfPermission(context, permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS) == PackageManager.PERMISSION_DENIED)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; final WhiteListedInBatteryOptimizations appIsWhiteListedFromPowerSave = getIfAppIsWhiteListedFromBatteryOptimizations(context, packageName);&nbsp; &nbsp; &nbsp; &nbsp; Intent intent = null;&nbsp; &nbsp; &nbsp; &nbsp; switch (appIsWhiteListedFromPowerSave) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case WHITE_LISTED:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (alsoWhenWhiteListed)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent = new Intent(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case NOT_WHITE_LISTED:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS).setData(Uri.parse("package:" + packageName));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case ERROR_GETTING_STATE:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case UNKNOWN_TOO_OLD_ANDROID_API_FOR_CHECKING:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case IRRELEVANT_OLD_ANDROID_API:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return intent;&nbsp; &nbsp; }&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* registers a receiver to listen to power-save events. returns true iff succeeded to register the broadcastReceiver.&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; @TargetApi(VERSION_CODES.M)&nbsp; &nbsp; public static boolean registerPowerSaveReceiver(@NonNull Context context, @NonNull BroadcastReceiver receiver) {&nbsp; &nbsp; &nbsp; &nbsp; if (VERSION.SDK_INT < VERSION_CODES.M)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; IntentFilter filter = new IntentFilter();&nbsp; &nbsp; &nbsp; &nbsp; filter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);&nbsp; &nbsp; &nbsp; &nbsp; context.registerReceiver(receiver, filter);&nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP