下面列出了怎么用android.os.health.UidHealthStats的API类实例代码及写法,或者点击链接到github查看源代码。
@TargetApi(Build.VERSION_CODES.N)
@SuppressWarnings("CatchGeneralException")
// because takeMyUidSnapshot wraps RemoteException in a RuntimeException
private void snapshotAndLogMetrics() {
if (healthManager == null || mainHandler == null || !FitbitGatt.atLeastSDK(Build.VERSION_CODES.N)) {
Timber.v("Heath logging not enabled.");
return;
}
HealthStats currentStats = null;
try {
currentStats = healthManager.takeMyUidSnapshot();
} catch (RuntimeException e) {
Timber.tag(TAG).e(e, "Couldn't snapshot");
}
if (currentStats == null) {
Timber.tag(TAG).w("Couldn't log snapshot");
return;
}
long oldTxBytes = btTxBytes.getAndSet(currentStats.getMeasurement(UidHealthStats.MEASUREMENT_BLUETOOTH_TX_BYTES));
long oldRxBytes = btRxBytes.getAndSet(currentStats.getMeasurement(UidHealthStats.MEASUREMENT_BLUETOOTH_RX_BYTES));
long oldMamsValue = bluetoothPowerMilliampMilliseconds.getAndAdd(currentStats.getMeasurement(UidHealthStats.MEASUREMENT_BLUETOOTH_POWER_MAMS));
float oldMah = ((((float) oldMamsValue / 1000) * 60) * 60);
float newMah = ((((float) bluetoothPowerMilliampMilliseconds.get() / 1000) * 60) * 60);
mainHandler.postDelayed(this::snapshotAndLogMetrics, SNAPSHOT_INTERVAL);
Timber
.tag(TAG)
.i("Old BTLE TX: %d, new BTLE TX: %d. Old BTLE RX: %d, new BTLE RX: %d. Old BTLE mAh: %f, new BTLE mAh: %f",
oldTxBytes, btTxBytes.get(), oldRxBytes, btRxBytes.get(), oldMah, newMah);
}
/**
* Gets a HealthStatsParceler for the given uid. You should probably call
* updateExternalStatsSync first.
*/
HealthStatsParceler getHealthStatsForUidLocked(int requestUid) {
final HealthStatsBatteryStatsWriter writer = new HealthStatsBatteryStatsWriter();
final HealthStatsWriter uidWriter = new HealthStatsWriter(UidHealthStats.CONSTANTS);
final BatteryStats.Uid uid = mStats.getUidStats().get(requestUid);
if (uid != null) {
writer.writeUid(uidWriter, mStats, uid);
}
return new HealthStatsParceler(uidWriter);
}
private static void readKeyNames() {
try {
Class[] healthStatsClasses = {
UidHealthStats.class,
PidHealthStats.class,
ProcessHealthStats.class,
PackageHealthStats.class,
ServiceHealthStats.class
};
Class annotationClass = Class.forName("android.os.health.HealthKeys$Constant");
for (Class clazz : healthStatsClasses) {
Field[] fields = clazz.getFields();
for (Field field : fields) {
if (field.isAnnotationPresent(annotationClass)) {
sKeyNames.put(field.getInt(null), field.getName());
}
}
}
return;
} catch (IllegalAccessException iae) {
SystemMetricsLogger.wtf(TAG, "Unable to read constant names", iae);
} catch (ClassNotFoundException cnfe) {
SystemMetricsLogger.wtf(TAG, "Unable to find constant annotation", cnfe);
}
// Mark as attempted and invalid
sKeyNames.put(-1, "Unable to read");
}
@Test
public void testDiffWithReset() {
HealthStatsMetrics a = createTestMetrics();
a.measurement.put(UidHealthStats.MEASUREMENT_REALTIME_BATTERY_MS, 100L);
HealthStatsMetrics b = createTestMetrics();
b.measurement.put(UidHealthStats.MEASUREMENT_REALTIME_BATTERY_MS, 200L);
HealthStatsMetrics output = a.diff(b, null);
HealthStatsMetrics expectedOutput = createTestMetrics();
expectedOutput.measurement.put(UidHealthStats.MEASUREMENT_REALTIME_BATTERY_MS, 100L);
assertThat(output).isEqualTo(expectedOutput);
}
/** Checks the age difference of snapshots, similar to String comparisons. */
private static long compareSnapshotAge(HealthStatsMetrics a, HealthStatsMetrics b) {
long aRealtimeBatteryMs = a.measurement.get(UidHealthStats.MEASUREMENT_REALTIME_BATTERY_MS, 0L);
long bRealtimeBatteryMs = b.measurement.get(UidHealthStats.MEASUREMENT_REALTIME_BATTERY_MS, 0L);
return aRealtimeBatteryMs - bRealtimeBatteryMs;
}