Doze模式是为了节省电池而设计的。您应该将您的应用程序加入白名单以停用Doze模式。
来源:https://developer.android.com/training/monitoring-device-state/doze-standby
Support for other use cases Almost all apps should be able to support Doze by managing network connectivity, alarms, jobs, and syncs properly, and by using FCM high-priority messages. For a narrow set of use cases, this might not be sufficient. For such cases, the system provides a configurable whitelist of apps that are partially exempt from Doze and App Standby optimizations.
An app that is whitelisted can use the network and hold partial wake locks during Doze and App Standby. However, other restrictions
still apply to the whitelisted app, just as they do to other apps. For example, the whitelisted app’s jobs and syncs are deferred (on API level 23 and below), and its regular AlarmManager alarms do not fire. An app can check whether it is currently on the exemption whitelist by calling isIgnoringBatteryOptimizations().
以下是如何将您的应用程序添加到白名单中: 1. 步骤-->在您的xml文件中添加此权限。
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
2. 忽略电池优化
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent intent = new Intent();
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
}
}
workermanager.equeue()
直到我解锁设备才会触发),即使在电池优化白名单上。值得一提的是,我在Pixel XL(安卓10)上进行测试。 - lasec0203