一加5设备忽略电池优化功能失效问题技术求助
I’ve run into this exact headache with OnePlus devices running Oxygen OS 5.0.1—their heavily customized battery management system completely ignores the standard Android battery optimization exemption request, even after the user grants it. Let’s break down why this happens and how to fix it:
Why the Standard Method Fails
OnePlus layered extra battery optimization controls on top of Android 8.0’s native system. The ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS intent only handles a partial exemption, but the device’s additional "Deep Sleep" or "Smart Power Saver" rules still block background broadcasts like SMS_RECEIVED unless you explicitly disable them through OnePlus’s dedicated settings interface.
Step-by-Step Solution
1. Detect the Problematic Device/OS Combination
First, check if you’re dealing with the specific OnePlus configuration:
boolean isOnePlusOreo = Build.MANUFACTURER.equalsIgnoreCase("oneplus") && Build.VERSION.SDK_INT == Build.VERSION_CODES.O;
2. Redirect to OnePlus’s Custom Battery Optimization Page
Skip the standard Android intent and send users directly to OnePlus’s dedicated battery settings where they can fully disable restrictions for your app:
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE); boolean isIgnoringOptimizations = powerManager.isIgnoringBatteryOptimizations(getPackageName()); if (!isIgnoringOptimizations) { if (isOnePlusOreo) { try { // Launch OnePlus's custom battery optimization activity Intent intent = new Intent(); ComponentName componentName = new ComponentName( "com.oneplus.settings", "com.oneplus.settings.battery.BatteryOptimizationActivity" ); intent.setComponent(componentName); startActivity(intent); } catch (ActivityNotFoundException e) { // Fall back to standard Android settings if the custom page isn't available Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); intent.setData(Uri.parse("package:" + getPackageName())); startActivity(intent); } } else { // Standard flow for all other devices Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); intent.setData(Uri.parse("package:" + getPackageName())); startActivity(intent); } }
3. Enable Auto-Start Permission
On OnePlus devices, even with battery optimizations disabled, apps without auto-start permission can still miss background broadcasts. Guide users to enable this setting:
- Navigate to Settings > Apps & notifications > See all apps > [Your App Name] > Auto-start
- Toggle the switch to allow auto-start for your app
4. Test the Broadcast
After the user completes these steps, test the SMS_RECEIVED broadcast again. The app should now receive it reliably without being blocked by battery optimization rules.
内容的提问来源于stack exchange,提问作者Aalap

