You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Oreo系统获取各应用电池使用统计:Android 8.1是否提供对应API?

Getting Per-App Battery Usage Stats on Android Oreo (8.1)

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_SUMMARY intent only opens the system battery settings page—it can't retrieve data programmatically.
  • Regular third-party apps can only estimate battery usage using UsageStatsManager and global battery data.
  • Precise per-app battery stats require system-level privileges, using either BatteryStatsManager or reflection of internal classes.

内容的提问来源于stack exchange,提问作者narb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:39:49