手机电池电量

使用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();
    }
}


运行结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值