标签:
逻辑不难,主要代码如下
/** * 通过粘性广播检测电量 */ 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