使用BroadcastReceiver捕捉Intent.ACTION_BATTERY_CHANGED(电池电量变化时广播的消息)。BroadcastReceiver是一种全局监听器,接下来使用BroadcastReceiver来监听手机电池电量的变化。
strings.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, BatteryActivity!</string>
<string name="app_name">电池电量</string>
<string name="str_title">我的手机电量还剩...</string>
<string name="str_button1">按我取得电池计量</string>
<string name="str_button2">返回</string>
<string name="str_dialog_title">系统信息</string>
<string name="str_dialog_body">电池计量:</string>
</resources>
mian.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/myTextView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/holo_blue_light"
android:textSize="20sp"
android:text="@string/str_title"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/str_button1"
android:id="@+id/battery"
/>
</LinearLayout>
BatteryActivity.java,代码如下:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class BatteryActivity extends Activity {
/** Called when the activity is first created. */
private int intLevel,intScale;
private Button battery;
private AlertDialog alert;
private BroadcastReceiver broadcast=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action=intent.getAction();
if(Intent.ACTION_BATTERY_CHANGED.equals(action)){
intLevel=intent.getIntExtra("level",0);
intScale=intent.getIntExtra("scale", 100);
onBatteryInfoReceiver(intLevel, intScale);
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
battery=(Button) findViewById(R.id.battery);
battery.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
registerReceiver(broadcast, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
});
}
public void onBatteryInfoReceiver(int intLevel,int intScale){
alert=new AlertDialog.Builder(BatteryActivity.this).create();
alert.setTitle(R.string.str_dialog_title);
alert.setMessage(getResources().getString(R.string.str_dialog_body)+String.valueOf(intLevel*100/intScale)+"%");
alert.setButton(getResources().getString(R.string.str_button2),new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
unregisterReceiver(broadcast);
alert.dismiss();
}
});
alert.show();
}
}
运行结果: