たぶんAndroid 10から追加されたバッテリーの残り時間をアプリで取得できたのでメモ。
Settings.Global
に格納されていて、ContentResolverでアクセスできる。キーはandroid.provider.Settingsに定義があったけれど、@hideされていたのでコピペして使った。
private val BATTERY_ESTIMATES_LAST_UPDATE_TIME = "battery_estimates_last_update_time" private val TIME_REMAINING_ESTIMATE_MILLIS = "time_remaining_estimate_millis" private val TIME_REMAINING_ESTIMATE_BASED_ON_USAGE = "time_remaining_estimate_based_on_usage" private val AVERAGE_TIME_TO_DISCHARGE = "average_time_to_discharge" private val AVERAGE_TIME_TO_DISCHARGE_UNKNOWN = -1 private val ESTIMATE_MILLIS_UNKNOWN = -1 data class Estimate( val lastUpdateTime: Long, val estimateMillis: Long, val isEstimateBasedOnUsage: Boolean, val timeToDischarge: Long, ) fun getEstimate() = Estimate( Settings.Global.getLong( contentResolver, BATTERY_ESTIMATES_LAST_UPDATE_TIME, -1 ), Settings.Global.getLong( contentResolver, TIME_REMAINING_ESTIMATE_MILLIS, ESTIMATE_MILLIS_UNKNOWN.toLong() ), Settings.Global.getInt( contentResolver, TIME_REMAINING_ESTIMATE_BASED_ON_USAGE, 0 ) == 1, Settings.Global.getLong( contentResolver, AVERAGE_TIME_TO_DISCHARGE, AVERAGE_TIME_TO_DISCHARGE_UNKNOWN.toLong() ) )