●バッテリーの状態を取得する

BatteryManagerからバッテリーの情報を取得できます。


public class MainActivity extends AppCompatActivity {

    android.widget.TextView view;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        view=new android.widget.TextView(this);

        setContentView(view);

        java.util.TimerTask timertask = new java.util.TimerTask(){
            android.os.Handler handler = new android.os.Handler();
            @Override
            public void run() {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        view.setText(getStatus());
                    }
                });
            }
        };
        java.util.Timer timer = new java.util.Timer(true);
        timer.schedule(timertask,(long)2000,(long)1000);
    }

    private String getStatus(){

        String str = "";
        IntentFilter intentfilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent batteryStatus = this.registerReceiver(null, intentfilter );

        //バッテリーの健康状態
        int bh = batteryStatus.getIntExtra(BatteryManager.EXTRA_HEALTH, -1);
        if(bh == BatteryManager.BATTERY_HEALTH_GOOD){
            str = str + "良好\n";
        } else if(bh == BatteryManager.BATTERY_HEALTH_DEAD){
            str = str + "壊れている\n";
        } else if(bh == BatteryManager.BATTERY_HEALTH_COLD){
            str = str + "低温状態\n";
        } else if(bh == BatteryManager.BATTERY_HEALTH_OVERHEAT){
            str = str + "温度異常\n";
        } else if(bh == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE){
            str = str + "電圧異常\n";
        } else if(bh == BatteryManager.BATTERY_HEALTH_UNKNOWN){
            str = str + "不明\n";
        } else if(bh == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE){
            str = str + "不明";
        }

        //リソースID
        int bi = batteryStatus.getIntExtra(BatteryManager.EXTRA_ICON_SMALL, -1);
        str = str + "ID " + bi + "\n";

        //電池残量
        int Level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int MaxLevel = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        str = str + "電池残量 " + Level * 100 / MaxLevel + "%\n";

        //電源接続状態
        int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        if(chargePlug == BatteryManager.BATTERY_PLUGGED_USB){
            str = str + "USB電源接続\n";
        }else if(chargePlug == BatteryManager.BATTERY_PLUGGED_AC){
            str = str + "AC電源接続\n";
        }else if(chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS) {
            str = str + "ワイヤレス電源接続\n";
        }else{
            str = str + "電源接続無し\n";
        }

        //バッテリーの有無
        boolean bpr = batteryStatus.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false);
        if(bpr){
            str = str + "バッテリー有り\n";
        }else{
            str = str + "バッテリー無し\n";
        }

        //バッテリーの状態
        int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        if(status == BatteryManager.BATTERY_STATUS_CHARGING){
            str = str + "充電中\n";
        }else if(status == BatteryManager.BATTERY_STATUS_FULL){
            str = str + "充電満タン\n";
        }else if(status == BatteryManager.BATTERY_STATUS_DISCHARGING){
            str = str + "放電中\n";
        }else if(status == BatteryManager.BATTERY_STATUS_NOT_CHARGING){
            str = str + "充電停止\n";
        }else{
            str = str + "充電状態不明\n";
        }

        //バッテリーの種類
        str=str + batteryStatus.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY)+"\n";

        //バッテリーの温度
        float temp = (float)batteryStatus.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
        str = str + temp / 10 + " ℃\n";

        //バッテリーの電圧
        float volt = (float)batteryStatus.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
        str = str + volt / 1000 + " V\n";

        return str;
    }


}

しかしながら、BatteryManagerからでは電流を取得することができません
もっと奇妙奇天烈な事をする必要があるみたいです。

端末の電流を取得はこちらへ

▲トップページ > android