package com.example.hellow;
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;
public class MainActivity extends Activity { TextView plugged; TextView status; TextView level;
private BroadcastReceiver receiver = new BroadcastReceiver(){ private Intent intent; public void onReceive(Context context, Intent intent){ this.intent = intent; plugged.setText(getPlugged()); status.setText(getStatus()); level.setText(getLevel());
}
public String getPlugged(){ int plug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0); String pluggedStr = ""; switch(plug){ case BatteryManager.BATTERY_PLUGGED_AC : pluggedStr = "BATTERY_PLUGGED_AC"; break; case BatteryManager.BATTERY_PLUGGED_USB : pluggedStr = "BATTERY_PLUGGED_USB"; break; } return pluggedStr; }
public String getStatus(){ int plug = intent.getIntExtra(BatteryManager.EXTRA_STATUS, 0); String statusStr = ""; switch(plug){ case BatteryManager.BATTERY_STATUS_CHARGING : statusStr = "BATTERY_STATUS_CHARGING"; break; case BatteryManager.BATTERY_STATUS_DISCHARGING : statusStr = "BATTERY_STATUS_DISCHARGING"; break; case BatteryManager.BATTERY_STATUS_UNKNOWN : statusStr = "BATTERY_STATUS_UNKNOWN"; break; case BatteryManager.BATTERY_STATUS_FULL : statusStr = "BATTERY_STATUS_FULL"; break; case BatteryManager.BATTERY_STATUS_NOT_CHARGING : statusStr = "BATTERY_STATUS_NOT_CHARGING"; break; } return statusStr;
}
public String getLevel(){ int lv = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0); String levelStr = "" + lv + "%"; return levelStr;
}
};
@Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.batterystatus); plugged = (TextView)findViewById(R.id.txt_plugged); status = (TextView)findViewById(R.id.txt_status); level = (TextView)findViewById(R.id.txt_level); //브로드캐스트 리시버를 등록 //첫번째 방법. manifest.xml에 등록 //두번째 방법. activity에서 registerReceiver() 등록 //배터리 상태가 변경되면 registerReceiver(receiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED)); }
@Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); unregisterReceiver(receiver); }
}
-----------------------------------------------------------------------------------------------
batterystatus.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >
<TextView android:id="@+id/txt_plugged" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<TextView android:id="@+id/txt_status" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<TextView android:id="@+id/txt_level" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
|