码迷,mamicode.com
首页 > 移动开发 > 详细

Android通过StickyBroadcast进行低电量检测提示

时间:2015-02-01 12:18:23      阅读:600      评论:0      收藏:0      [点我收藏+]

标签:

逻辑不难,主要代码如下

/**
	 * 通过粘性广播检测电量
	 */
	private void checkBattery()
	{
		//通过粘性广播读取电量
		IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
		Intent intentBattery = registerReceiver(null, intentFilter);//注意,粘性广播不需要广播接收器
		if(intentBattery!=null)
		{
			//获取当前电量  
			int batteryLevel = intentBattery.getIntExtra("level", 0);  
		    //电量的总刻度  
		    int batterySum = intentBattery.getIntExtra("scale", 100);  
			float rotatio = 100*(float)batteryLevel/(float)batterySum;
			LogUtils.d("currentBattery="+rotatio+"%");
			if(rotatio<15)
			{
				getWindow().getDecorView().postDelayed(new Runnable() {
					@Override
					public void run() {
						showAlertToastTip(getString(R.string.common_low_batter));
					}
				}, 100);
			}
		}
	}
	/**
	 * 显示警告提示
	 * @param msg
	 */
	private void showAlertToastTip(String msg)
	{
		TextView msgTv = null;
		Toast toast = null;
		toast = new Toast(this);
		toast.setDuration(Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.TOP | Gravity.FILL_HORIZONTAL, 0, 0);

		View toastView = LayoutInflater.from(this).inflate(
					R.layout.common_simple_toast_layout, null);
		msgTv = (TextView) toastView.findViewById(R.id.common_toast_text_tv);
		toastView.setTag(msgTv);
		toast.setView(toastView);
		msgTv.setText(msg);
		toast.show();
	}


Android通过StickyBroadcast进行低电量检测提示

标签:

原文地址:http://my.oschina.net/ososchina/blog/374051

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!