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.os.Bundle;import android.util.Log;import android.view.View;import android.view.Window;import android.view.WindowManager;import android.widget.Button;import android.widget.TextView;public class BatteryInfo extends Activity{  /* 变量声明 */  private int intLevel;  private int intScale;  private Button mButton01;  /* 创建BroadcastReceiver */  private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver()  {    public void onReceive(Context context, Intent intent)    {      String action = intent.getAction();      /*       * 如果捕捉到的action是ACTION_BATTERY_CHANGED, 就运行onBatteryInfoReceiver()       */      if (Intent.ACTION_BATTERY_CHANGED.equals(action))      {        intLevel = intent.getIntExtra("level", 0);        intScale = intent.getIntExtra("scale", 100);        Log.i("", "intLevel = " + intLevel);        Log.i("", "intScale = " + intScale);        onBatteryInfoReceiver(intLevel, intScale);      }    }  };  /** Called when the activity is first created. */  @Override  public void onCreate(Bundle savedInstanceState)  {    super.onCreate(savedInstanceState);    setContentView(R.layout.main);    mButton01 = (Button) findViewById(R.id.myButton1);    mButton01.setOnClickListener(new Button.OnClickListener()    {      public void onClick(View v)      {        // 注册一个系统 BroadcastReceiver,作为访问电池计量之用        // 這個不能直接在AndroidManifest.xml中註冊        registerReceiver(mBatInfoReceiver, new IntentFilter(            Intent.ACTION_BATTERY_CHANGED));      }    });  }  /* 捕捉到ACTION_BATTERY_CHANGED时要运行的method */  public void onBatteryInfoReceiver(int intLevel, int intScale)  {    /* create 跳出的对话窗口 */    final Dialog d = new Dialog(BatteryInfo.this);    d.setTitle(R.string.str_dialog_title);    d.setContentView(R.layout.mydialog);    /* 创建一个背景模糊的Window,且将对话窗口放在前景 */    Window window = d.getWindow();    window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,        WindowManager.LayoutParams.FLAG_BLUR_BEHIND);    /* 将取得的电池计量显示于Dialog中 */    TextView mTextView02 = (TextView) d.findViewById(R.id.myTextView2);    mTextView02.setText(getResources()        .getText(R.string.str_dialog_body)        + String.valueOf(intLevel * 100 / intScale) + "%");    /* 设置返回主画面的按钮 */    Button mButton02 = (Button) d.findViewById(R.id.myButton2);    mButton02.setOnClickListener(new Button.OnClickListener()    {      public void onClick(View v)      {        /* 反注册Receiver,并关闭对话窗口 */        unregisterReceiver(mBatInfoReceiver);        d.dismiss();      }    });    d.show();  }}

代码里还用到了自定义的Dialog布局:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:background="@drawable/white" android:orientation="vertical"android:layout_width="fill_parent" android:layout_height="fill_parent"><TextView android:id="@+id/myTextView2" android:layout_width="fill_parent"android:layout_height="wrap_content" android:textColor="@drawable/blue"android:textSize="16sp" android:gravity="center" android:padding="10px"></TextView><Button android:id="@+id/myButton2" android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="@string/str_button2"></Button></LinearLayout>


http://edison-cool911.iteye.com/blog/737781

更多相关文章

  1. 全局窗口二
  2. Android(安卓)之欢迎界面Splash
  3. ch07 Android(安卓)日期与时间对话框
  4. Android日期对话框
  5. Android中Activity, View,Window,DecorView之间的关系
  6. android 自定义基于组件的注册用户对话框
  7. android 返回键退出程序
  8. android悬浮球,应用在所有软件之上,点击可以显示菜单
  9. 十分钟让你了解Android触摸事件原理(InputManagerService)

随机推荐

  1. network: Android(安卓)网络判断(wifi、3G
  2. Android(安卓)打开关闭闪光灯(手电筒)
  3. Android(安卓)使用系统摄像头拍相片与拍
  4. Android(安卓)SQLite Database
  5. android一种较为复杂的布局
  6. android 之 选择文件
  7. android xml本地数据弹窗多选
  8. Android(安卓)WebView加载https网页(亲测)
  9. Android之setResult的用法
  10. android(linux) 背光流程