package com.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 BatteryBroadcastRecieverDemo 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)); } // バッテリ情報を受信するブロードキャストレシーバー // 通常はBroadcastReceiverを継承して作るが、簡易的に匿名内部クラスで記述して、即オブジェクトを生成 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); } } }; }