Oreo系统获取各应用电池使用统计:Android 8.1是否提供对应API?
Hey there! Let's break down how to tackle this since you've found the android.intent.action.POWER_USAGE_SUMMARY intent way too limited for your needs. First, a quick clarification: Android 8.1 didn't introduce a brand-new dedicated API for per-app battery usage. The framework for accessing this data was refined in Android 8.0 (Oreo), but getting precise app-specific stats still depends heavily on your app's privileges. Here are the feasible approaches:
1. For Regular Third-Party Apps (No System Privileges)
Regular apps can't access raw, precise battery consumption data for other apps (or even their own, beyond rough estimates). But you can combine app usage metrics with global battery data to make educated guesses about battery drain:
Step 1: Request Usage Access Permission
You'll need the android.permission.PACKAGE_USAGE_STATS permission, which requires users to manually grant access through system settings:
// Check if we already have usage access permission AppOpsManager appOps = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE); int mode = appOps.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, android.os.Process.myUid(), getPackageName()); boolean hasPermission = mode == AppOpsManager.MODE_ALLOWED; if (!hasPermission) { // Redirect user to enable usage access Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS); startActivity(intent); }
Step 2: Fetch Usage Stats for Estimation
With permission granted, pull app foreground time and other usage metrics to pair with global battery changes:
UsageStatsManager usageStatsManager = (UsageStatsManager) getSystemService(Context.USAGE_STATS_SERVICE); long endTime = System.currentTimeMillis(); long startTime = endTime - (1000 * 60 * 60 * 24); // Last 24 hours List<UsageStats> usageStatsList = usageStatsManager.queryUsageStats( UsageStatsManager.INTERVAL_DAILY, startTime, endTime); // Iterate through stats to get per-app data for (UsageStats stats : usageStatsList) { String packageName = stats.getPackageName(); long foregroundTime = stats.getTotalTimeInForeground(); // Combine this with global battery level changes to estimate drain // (Note: This is an estimate, not precise battery usage) }
2. For System Apps / Privileged Apps
If your app has system signing or privileged access (e.g., pre-installed by an OEM), you can access precise battery stats using internal framework tools:
Option A: Use BatteryStatsManager (Android 8.0+)
Android 8.0 introduced BatteryStatsManager which provides access to detailed battery usage data:
// Requires system privileges or signature permission BatteryStatsManager batteryStatsManager = (BatteryStatsManager) getSystemService(Context.BATTERY_STATS_SERVICE); BatteryStats batteryStats = batteryStatsManager.getActiveStatistics(); // Iterate through UID-level stats (each UID maps to one or more apps) for (int i = 0; i < batteryStats.getAppCount(); i++) { BatteryStats.Uid uidStats = batteryStats.getUidStats(i); String packageName = getPackageManager().getNameForUid(uidStats.getUid()); // Get granular metrics long cpuTime = uidStats.getCpuTime(); long wakeLockTime = uidStats.getWakeLockTime(); long mobileNetworkUsage = uidStats.getMobileRadioDataBytes(); // Calculate approximate battery drain using the device's PowerProfile // (You'll need to reference the device-specific power profile for accurate calculations) }
Option B: Reflect Internal BatteryStats Class
If BatteryStatsManager isn't sufficient, you can reflect the hidden com.android.internal.os.BatteryStats class to get even more detailed data. Note that this is unofficially supported and may break across Android versions.
Key Takeaways
- The
android.intent.action.POWER_USAGE_SUMMARYintent only opens the system battery settings page—it can't retrieve data programmatically. - Regular third-party apps can only estimate battery usage using
UsageStatsManagerand global battery data. - Precise per-app battery stats require system-level privileges, using either
BatteryStatsManageror reflection of internal classes.
内容的提问来源于stack exchange,提问作者narb

