效果图:
activity代码:
- package cn.com.chenzheng_java;
- 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.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.Window;
- import android.view.WindowManager;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup.LayoutParams;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- /***
- * @description 查看手机电池电量,这里要注意监听Intent.ACTION_BATTERY_CHANGED的广播接收者,只能
- * 在程序中显示的注册,不能再androiManifest.xml中进行静态的注册
- * @author chenzheng_java
- * @since 2011/03/16
- *
- */
- public class BatteryActivity extends Activity implements OnClickListener{
- // 当系统处于充电状态或者电量发生改变时会广播该action
- private static final String ACTION_NAME = Intent.ACTION_BATTERY_CHANGED;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.battery);
- Button button = (Button) findViewById(R.id.button1);
- // 注意哦,我们这里的activity实现了View.OnClickListener接口
- button.setOnClickListener(this);
- }
- // 声明广播接收者
- BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- String actionName = intent.getAction();
- if(actionName.equals(ACTION_NAME)){
- int level = intent.getIntExtra("level", 0);
- int scale = intent.getIntExtra("scale", 100);
- // Toast.makeText(BatteryActivity.this, "level="+level+" scale="+scale, Toast.LENGTH_LONG).show();
- final Dialog dialog = new Dialog(BatteryActivity.this);
- dialog.setTitle("电量显示");
- // 加载xml布局文件为View对象
- LayoutInflater flater = LayoutInflater.from(BatteryActivity.this);
- View view = flater.inflate(R.layout.battery2, null);
- dialog.setContentView(view,new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
- /**
- * 获取该dialog显示的父窗口,然后设置当dialog显示时,窗口内的其他部分设置为模糊
- * WindowManager.LayoutParams.FLAG_BLUR_BEHIND告诉我们,无论什么对象显示于前段,
- * 都会显示在window的最上层
- */
- Window window = dialog.getWindow();
- window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
- /**
- * 这里注意findViewById()方法,默认的会去加载main.xml中的组件。如果你想加载x.xml中的id为
- * a的组件,那么应该在前面加上setContentView这个布局文件的对象,如这里的dialog
- */
- TextView textView = (TextView)dialog.findViewById(R.id.textView_message);
- textView.setText("目前的手机电量为"+level*100/scale+"%");
- Button button2 = (Button) dialog.findViewById(R.id.button_back);
- button2.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- dialog.dismiss();
- // 注销广播接收者
- unregisterReceiver(broadcastReceiver);
- }
- });
- dialog.show();
- }
- }
- };
- @Override
- public void onClick(View v) {
- IntentFilter filter = new IntentFilter(ACTION_NAME);
- registerReceiver(broadcastReceiver, filter);
- }
- }
battery2.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:text="TextView" android:id="@+id/textView_message"
- android:textSize="10sp"
- android:gravity="center"
- android:padding="10px"
- android:layout_width="fill_parent" android:layout_height="wrap_content"></TextView>
- <Button android:id="@+id/button_back" android:layout_width="wrap_content"
- android:text="返回" android:layout_height="wrap_content"></Button>
- </LinearLayout>
battery.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">
- <Button android:text="显示电量" android:id="@+id/button1"
- android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
- </LinearLayout>