# AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bitacademy.broadcast"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".MonitorBatteryActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="BatteryReceiver">
<intent-filter>
<action
android:name="android.intent.action.BATTERY_CHANGED"></action>
<action
android:name="android.intent.action.BATTERY_LOW"></action>
<action
android:name="android.intent.action.BATTERY_OKAY"></action>
<action
android:name="android.intent.action.ACTION_POWER_CONNECTED"></action>
<action
android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
</intent-filter>
</receiver>
</application>
<uses-sdk
android:minSdkVersion="8" />
</manifest>
# main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
# MonitoryBatteryActivity.java
package com.bitacademy.broadcast;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class MonitorBatteryActivity extends Activity {
TextView mStatus ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mStatus = (TextView)findViewById(R.id.status);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
unregisterReceiver(mBatteryReceiver);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
intentFilter.addAction(Intent.ACTION_BATTERY_LOW);
intentFilter.addAction(Intent.ACTION_BATTERY_OKAY);
intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);
intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);
registerReceiver(mBatteryReceiver, intentFilter);
}
BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
int count = 0;
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
onBatteryChanged(intent);
}
if (action.equals(Intent.ACTION_BATTERY_LOW)) {
Toast.makeText(context, "배터리 용량 부족",
Toast.LENGTH_LONG).show();
}
if (action.equals(Intent.ACTION_BATTERY_OKAY)) {
Toast.makeText(context, "배터리 용량 정상",
Toast.LENGTH_LONG).show();
}
if (action.equals(Intent.ACTION_POWER_CONNECTED)) {
Toast.makeText(context, "전원 연결됨",
Toast.LENGTH_LONG).show();
}
if (action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
Toast.makeText(context, "전원 접속 끊김",
Toast.LENGTH_LONG).show();
}
}
public void onBatteryChanged(Intent intent) {
int plug, status, scale, level, ratio;
String plugType = "";
String batteryStatus = "";
if (intent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, false) == false) {
mStatus.setText("배터리 없음");
return;
}
plug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,
BatteryManager.BATTERY_STATUS_UNKNOWN);
scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
ratio = level * 100 / scale;
if (plug == BatteryManager.BATTERY_PLUGGED_AC) {
plugType = "AC";
}
else if (plug == BatteryManager.BATTERY_PLUGGED_USB) {
plugType = "USB";
}
else {
plugType = "Battery";
}
if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
batteryStatus = "현재 충전중";
}
else if (status == BatteryManager.BATTERY_STATUS_FULL) {
batteryStatus = "완전 충전됨";
}
else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
batteryStatus = "현재 충전중이 아님";
}
else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
batteryStatus = "현재 방전중";
}
else if (status == BatteryManager.BATTERY_STATUS_UNKNOWN) {
batteryStatus = "알 수 없음";
}
String str = "수신 횟수 : " + count + "\n 연결 : " + plugType +
"\n 상태 : " + batteryStatus + "\n 레벨 : " + ratio;
mStatus.setText(str);
}
};
}