2016-07-12 21 views
-1

私は実際の電流、電圧のようなアンドロイドデバイスのバッテリーに関するいくつかの情報を取得しようとします... 私は電圧、レベルを得ることに成功...しかし、残念ながら私はAPI 16とパラメータBatteryManager.BATTERY_PROPERTY_CURRENT_NOWはAPI 21で表示されるため、このAPIには存在しません。バッテリーの情報をアンドロイドデバイスから取得

このデバイスの現在の情報とバッテリ容量の情報を取得するにはどうすればよいですか?

ありがとうございます。

+0

コードを請求していますか? –

答えて

0

BatteryManagerを試すことができます。それはUSBまたはワイヤレスで、バッテリーの健康とより

0

ように充電された場合、それはあなたが、ステータス、容量を(完全には、充放電)を取得することができ、それによってAPI 16

https://developer.android.com/reference/android/os/BatteryManager.html

で動作しますBatteryManagerはAPI 16で動作します。次のutilsは便利です。

package com.example.utils; 

import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.BatteryManager; 

import com.example.app.AppContext; 
import com.example.app.AppManager; 

public class BatteryUtils { 

    public static Intent getBatteryIntent() { 
     IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); 
     return AppContext.getInstance().registerReceiver(null, ifilter); 
    } 

    public static int getScale(Intent intent) { 
     return intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1); 
    } 

    public static int getLevel(Intent intent) { 
     return intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); 
    } 

    public static int getChargeStatus(Intent intent) { 
     return intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1); 
    } 

    public static boolean isCharging(int status) { 
     return status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL; 
    } 

    public static boolean isCharging(Intent intent) { 
     int status = getChargeStatus(intent); 
     return isCharging(status); 
    } 

    public static void test() { 
     Intent intent = BatteryUtils.getBatteryIntent(); 
     DebugUtils.log("isCharging:" + BatteryUtils.isCharging(intent) + ";level:" + BatteryUtils.getLevel(intent) + ";Scale:" + BatteryUtils.getScale(intent)); 
    } 

} 
関連する問題