▼Androidメモ▼
バッテリー情報の取得


バッテリー情報を取得するプログラムを作成する。



ソースコード
BatteryEx.java
package net.npaka.batteryex;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.BatteryManager;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

//バッテリー情報の取得
public class BatteryEx extends Activity {
    private TextView textView;
    
    //初期化
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        //レイアウトの生成
        LinearLayout layout=new LinearLayout(this);
        layout.setBackgroundColor(Color.rgb(255,255,255));
        layout.setOrientation(LinearLayout.VERTICAL);
        setContentView(layout); 

        //テキストビューの生成
        textView=new TextView(this);
        textView.setText("BatteryEx");
        textView.setTextSize(16.0f);                  
        textView.setTextColor(Color.rgb(0,0,0));
        setLLParams(textView);
        layout.addView(textView);
    }  

    @Override
    protected void onResume() {
        super.onResume();
        
        //バッテリー情報の受信開始
        IntentFilter filter=new IntentFilter();
        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryReceiver,filter);
    }

    @Override
    protected void onPause() {
        super.onPause();
        
        //バッテリー情報の受信停止
        unregisterReceiver(batteryReceiver);
    }
    
    //ライナーレイアウトのパラメータ指定
    private static void setLLParams(View view) {
        view.setLayoutParams(new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
    }

    //バッテリ情報を受信するブロードキャストレシーバー
    private BroadcastReceiver batteryReceiver=new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
                //充電状態の取得
                String statusStr="";
                int status=intent.getIntExtra("status",0);
                if (status==BatteryManager.BATTERY_STATUS_CHARGING) {
                    statusStr="充電中";
                } else if (status==BatteryManager.BATTERY_STATUS_DISCHARGING) {
                    statusStr="充電切断";
                } else if (status==BatteryManager.BATTERY_STATUS_FULL) {
                    statusStr="充電満タン";
                } else if (status==BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
                    statusStr="充電切断中";
                } else if (status==BatteryManager.BATTERY_STATUS_UNKNOWN) {
                    statusStr="不明";
                } 
                
                //プラグ種別の取得
                String pluggedStr="";
                int plugged=intent.getIntExtra("plugged",0);
                if (plugged==BatteryManager.BATTERY_PLUGGED_AC) {
                    pluggedStr="ACアダプタ";
                } else if (plugged==BatteryManager.BATTERY_PLUGGED_USB) {
                    pluggedStr="USB";
                }

                //バッテリー量の取得
                int level=intent.getIntExtra("level",0);
                int scale=intent.getIntExtra("scale",0);

                //温度の取得
                int temperature=intent.getIntExtra("temperature",0);
                                
                String str="";
                str+="充電状態:"+statusStr+"\n";
                str+="プラグ種別:"+pluggedStr+"\n";
                str+="バッテリー量:"+level+"/"+scale+"\n";
                str+="温度:"+(temperature/10)+"度\n";
                textView.setText(str);
            }
        }
    };
}


−戻る−