【android开发笔记之电池低电处理】
![android开发笔记之电池低电处理](http://img.gansulong.com/231001/03233G2V-0.jpg)
文章插图
公司最近在搞个KMS,鼓励大家技术分享 。好吧,我是一个好员工,积极响应领导的各种要求 。但是,这个我不会写的,你可以说我没有分享精神,可以说我技术水平low,我就是不会写 。如果公司一定要强制要求我写,我也一定是乱写凑数,这完全不能怪我,只能怪这个无情的世界,无情的季节,无情的…………. 。(请不要问我具体的原因,谢谢!)
但是,有一个好朋友,在上面发表了一个电池低电处理的文章,我看了一下,确实是说的非常清楚,只是是一个比较低版本的代码流程,我在 7.1的代码上看了一下,和他说的有一点出入,不过,这没有关系不影响我们来学习 。
我可以不写,但是我们可以学习 。
.java
.//base//core/java/com///.java
当电池电量少于我们配置的低电量警告值()时,发送低电量通知,弹出低电量通知对话框 。
.//base/core/res/res//.xml
![android开发笔记之电池低电处理](http://img.gansulong.com/231001/03233MA3-1.jpg)
文章插图
15
只我满足的条件就可以发送低电量通知:
private boolean shouldSendBatteryLowLocked() {final boolean plugged = mPlugType != BATTERY_PLUGGED_NONE;final boolean oldPlugged = mLastPlugType != BATTERY_PLUGGED_NONE;/* The ACTION_BATTERY_LOW broadcast is sent in these situations:* - is just un-plugged (previously was plugged) and battery level is*less than or equal to WARNING, or* - is not plugged and battery level falls to WARNING boundary*(becomes <= mLowBatteryWarningLevel).*/return !plugged&& mBatteryProps.batteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN&& mBatteryProps.batteryLevel <= mLowBatteryWarningLevel&& (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);}
发送低电量通知.:
private void processValuesLocked(boolean force)if (shouldSendBatteryLowLocked()) {mSentLowBatteryBroadcast = true;mHandler.post(new Runnable() {@Overridepublic void run() {Intent statusIntent = new Intent(Intent.ACTION_BATTERY_LOW);statusIntent.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);mContext.sendBroadcastAsUser(statusIntent, UserHandle.ALL);}});
当电量为0时,发出关闭通知.OWN:
private void shutdownIfWeakChargerEmptySOCLocked() {if (mBatteryProps.batteryLevel == 0) {if (mInitiateShutdown) {if (ActivityManagerNative.isSystemReady()) {Intent intent = new Intent(Intent.ACTION_REQUEST_SHUTDOWN);intent.putExtra(Intent.EXTRA_KEY_CONFIRM, false);intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);mContext.startActivityAsUser(intent, UserHandle.CURRENT);}
nings.java
![android开发笔记之电池低电处理](http://img.gansulong.com/231001/03233J4R-2.jpg)
文章插图
.//base///src/com///power/nings.java
当电量低时,弹出低电量通知对话框:
private void showWarningNotification() {final int textRes = R.string.battery_low_percent_format;final String percentage = NumberFormat.getPercentInstance().format((double) mBatteryLevel / 100.0);final Notification.Builder nb = new Notification.Builder(mContext).setSmallIcon(R.drawable.ic_power_low)// Bump the notification when the bucket dropped..setWhen(mBucketDroppedNegativeTimeMs).setShowWhen(false).setContentTitle(mContext.getString(R.string.battery_low_title)).setContentText(mContext.getString(textRes, percentage)).setOnlyAlertOnce(true).setDeleteIntent(pendingBroadcast(ACTION_DISMISSED_WARNING)).setPriority(Notification.PRIORITY_MAX).setVisibility(Notification.VISIBILITY_PUBLIC).setColor(mContext.getColor(com.android.internal.R.color.battery_saver_mode_color));if (hasBatterySettings()) {nb.setContentIntent(pendingBroadcast(ACTION_SHOW_BATTERY_SETTINGS));}nb.addAction(0,mContext.getString(R.string.battery_saver_start_action),pendingBroadcast(ACTION_START_SAVER));if (mPlaySound) {attachLowBatterySound(nb);mPlaySound = false;}SystemUI.overrideNotificationAppName(mContext, nb);mNoMan.notifyAsUser(TAG_NOTIFICATION, R.id.notification_power, nb.build(), UserHandle.ALL);}
- 管理经济学 读书笔记 一
- Android进阶——Android跨进程通讯机制之Binder
- Android国际版WPS,助你解锁SVIP权益
- [Unity实践笔记] 整页翻、防误触的ScrollView
- 8.0 Android O Keyguard解锁流程
- 还以为敏捷开发是个概念?有人已经将它变为现实了!
- Android属性系统简介【转】
- 提升你的Android开发技能:从AR/VR沉浸到UI设计和故障排除
- Rookey.Frame v1.0快速开发平台-用户登录
- Android studio:Connection failed 网络配置错误