程序如下:
import android.app.Activity;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
public class A02Activity extends Activity {
private int level;
private int scale;
private Button b01;
private BroadcastReceiver mBatInfoReceiver=new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action=intent.getAction();
//如果捕捉到的是ACTION_BATTERY_CHANGED就运行onBatteryInfoReceiver();将电量显示于新窗口中
if(Intent.ACTION_BATTERY_CHANGED.equals(action)){
level=intent.getIntExtra("level", 0);
scale=intent.getIntExtra("scale", 100);
onBatteryInfoReceiver(level,scale);
}
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b01=(Button)findViewById(R.id.button01);
b01.setBackgroundColor(Color.GREEN);
b01.setText("查看电量");
b01.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
registerReceiver(mBatInfoReceiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
});
}
public void onBatteryInfoReceiver(int intLevel,int intScale){
final Dialog d=new Dialog(A02Activity.this);
d.setTitle(R.string.str_title);
d.setContentView(R.layout.dialog);
Window window=d.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
TextView mTextView02=(TextView)d.findViewById(R.id.myTextView02);
//取得电池电量显示于Dialog中
mTextView02.setText(getResources().getText(R.string.str_body)+String.valueOf(intLevel*100/intScale)+"%");
Button b02=(Button)d.findViewById(R.id.button02);
b02.setBackgroundColor(Color.RED);
b02.setText("返回");
b02.setTextColor(Color.YELLOW);
b02.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// 反注册Receiver并关闭窗口
unregisterReceiver(mBatInfoReceiver);
d.dismiss();
}
});
d.show();
}
}
在Android中,Android.intent.BATTERY_CHANGED是系统的Broadcast Action Message,当电池处于充电状态或电池电量有变化时,系统便会广播此Action;程序中的BroadcastReceiver在注册时,由于设置了Intent Filter过滤此Action信息,因此当BroadcastReceiver一被注册,就能马上捕捉这个Action,进而取得电池电量。
主程序中的onReceiver()是当BroadcastReceiver被触发时会运行的方法,写法如下:
public void onReceiver(Context context,Intent intent){
String action=intent.getAction();
if(Intent.ACTION_BATTERY_CHANGED.equals(action)){
/*运行程序的代码*/
}
}
添加这一判断Intent.ACTION_BATTERY_CHANGED.equals(action)是为了确保BroadcastReceiver只会被Intent.ACTION_BATTERY_CHANGED这个触发。如果没有这个判断程序也是可以运行的。
Android API中说明,要注册含有Intent.ACTION_BATTERY_CHANGED的Receiver,只能在程序中以Context.registerReceiver()方式来注册,不能直接在AndroidManifest.xml中注册。
本例中使用了让Dialog在弹出时,背景的窗口呈现模糊的状态:
final Dialog d=new Dialog(A02Activity.this);
d.setTitle(R.string.str_title);
d.setContentView(R.layout.dialog);
Window window=d.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
其中WindowManager.LayoutParams.FLAG_BLUR_BEHIND是告诉目前的Window不管是什么对象显示于前端,都会出现在Window的最上层,让背景Window呈现模糊状态。也可以在其他程序中使用这个效果。