日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android Low Battery 低电量处理流程

發布時間:2025/4/16 Android 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android Low Battery 低电量处理流程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

點擊打開鏈接

Android?通過BatteryService對電量進行自動管理。在BatteryService.java中從jni層獲得電量mBatteryLevel, ?并根據mBatteryLevel判斷手機電量是否過低,然后發出警告或聲音提醒,并且太低時還會自動關機。下面簡單介紹一下其流程。

? ? ? ?在BatteryService.java中判斷當前電量是否過低:

[java]?view plaincopy
  • /*?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).?
  • ?*/??
  • final?boolean?sendBatteryLow?=?!plugged??
  • ????????&&?mBatteryStatus?!=?BatteryManager.BATTERY_STATUS_UNKNOWN??
  • ????????&&?mBatteryLevel?<=?mLowBatteryWarningLevel??
  • ????????&&?(oldPlugged?||?mLastBatteryLevel?>?mLowBatteryWarningLevel);??
  • 如果當前電量小于警告電量(在config.xml中 <integer name="config_lowBatteryWarningLevel">15</integer>)則彈出電量低提示,或者電量為0(當然這個有誤差也可能是5%時就自動關機)時自動關機。如果低電量的話就發送一個廣播出去:

    [java]?view plaincopy
  • if?(sendBatteryLow)?{??
  • ????mSentLowBatteryBroadcast?=?true;??
  • ????statusIntent.setAction(Intent.ACTION_BATTERY_LOW);??
  • ????mContext.sendBroadcast(statusIntent);??
  • }??
  • ? ? ? ? 下面這段代碼是電量過低而自動關機:

    [java]?view plaincopy
  • private?void?More?...shutdownIfNoPowerLocked()?{??
  • ????//?shut?down?gracefully?if?our?battery?is?critically?low?and?we?are?not?powered.??
  • ????//?wait?until?the?system?has?booted?before?attempting?to?display?the?shutdown?dialog.??
  • ????if?(mBatteryLevel?==?0?&&?!isPoweredLocked(BatteryManager.BATTERY_PLUGGED_ANY))?{??
  • ????????mHandler.post(new?Runnable()?{??
  • ????????????@Override??
  • ????????????public?void?More?...run()?{??
  • ????????????????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);??
  • ????????????????}??
  • ????????????}??
  • ????????});??
  • ????}??
  • }??
  • ? ? ? ? 而在StatusBarPolicy.java中會接收廣播,?里面有判斷當接受到ACTION_BATTERY_LOW時, 調用onBatteryLow(intent)方法來處理:

    [java]?view plaincopy
  • private?BroadcastReceiver?mIntentReceiver?=?new?BroadcastReceiver()?{??
  • ????????@Override??
  • ????????public?void?onReceive(Context?context,?Intent?intent)?{??
  • ????????????String?action?=?intent.getAction();??
  • ????????????.....action.??
  • ????????????//里面會有判斷當接受到ACTION_BATTERY_LOW時:??
  • ????????????else?if?(action.equals(Intent.ACTION_BATTERY_LOW))?{??
  • ????????????????onBatteryLow(intent);??
  • ????????????}??
  • ????????}??
  • ????}??
  • ??????
  • ????private?void?onBatteryLow(Intent?intent)?{??
  • ????????if?(SHOW_LOW_BATTERY_WARNING)?{??
  • ????????????if?(false)?{??
  • ????????????????Slog.d(TAG,?"mPhoneState="?+?mPhoneState??
  • ????????????????????????+?"?mLowBatteryDialog="?+?mLowBatteryDialog??
  • ????????????????????????+?"?mBatteryShowLowOnEndCall="?+?mBatteryShowLowOnEndCall);??
  • ????????????}??
  • ??
  • ????????????if?(SHOW_BATTERY_WARNINGS_IN_CALL?||?mPhoneState?==?TelephonyManager.CALL_STATE_IDLE)?{??
  • ????????????????showLowBatteryWarning();??
  • ????????????}?else?{??
  • ????????????????mBatteryShowLowOnEndCall?=?true;??
  • ????????????}??
  • ????????}??
  • ????}??
  • ? ? ? ? ?彈出低電量過低Dialog提醒和聲音提醒:

    [java]?view plaincopy
  • private?void?showLowBatteryWarning()?{??
  • ????closeLastBatteryView();??
  • ??
  • ????//?Show?exact?battery?level.??
  • ????CharSequence?levelText?=?mContext.getString(R.string.battery_low_percent_format,?mBatteryLevel);??
  • ??
  • ????if?(mBatteryLevelTextView?!=?null)?{??
  • ????????mBatteryLevelTextView.setText(levelText);??
  • ????}?else?{??
  • ????????View?v?=?View.inflate(mContext,?R.layout.battery_low,?null);??
  • ????????mBatteryLevelTextView=(TextView)v.findViewById(R.id.level_percent);??
  • ????????mBatteryLevelTextView.setText(levelText);??
  • ????????AlertDialog.Builder?b?=?new?AlertDialog.Builder(mContext);??
  • ????????b.setCancelable(true);??
  • ????????b.setTitle(R.string.battery_low_title);??
  • ????????b.setView(v);??
  • ????????b.setIcon(android.R.drawable.ic_dialog_alert);??
  • ????????b.setPositiveButton(android.R.string.ok,?null);??
  • ??
  • ????????final?Intent?intent?=?new?Intent(Intent.ACTION_POWER_USAGE_SUMMARY);??
  • ????????intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK??
  • ????????????????|?Intent.FLAG_ACTIVITY_MULTIPLE_TASK??
  • ????????????????|?Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS??
  • ????????????????|?Intent.FLAG_ACTIVITY_NO_HISTORY);??
  • ????????if?(intent.resolveActivity(mContext.getPackageManager())?!=?null)?{??
  • ????????????b.setNegativeButton(R.string.battery_low_why,?new?DialogInterface.OnClickListener()?{??
  • ????????????????@Override??
  • ????????????????public?void?onClick(DialogInterface?dialog,?int?which)?{??
  • ????????????????????mContext.startActivity(intent);??
  • ????????????????????if?(mLowBatteryDialog?!=?null)?{??
  • ????????????????????????mLowBatteryDialog.dismiss();??
  • ????????????????????}??
  • ????????????????}??
  • ????????????});??
  • ????????}??
  • ??
  • ????????AlertDialog?d?=?b.create();??
  • ????????d.setOnDismissListener(mLowBatteryListener);??
  • ????????d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);??
  • ????????d.show();??
  • ????????mLowBatteryDialog?=?d;??
  • ????}??
  • ??????
  • ????//waring?voiced??
  • ????final?ContentResolver?cr?=?mContext.getContentResolver();??
  • ????if?(Settings.System.getInt(cr,?Settings.System.POWER_SOUNDS_ENABLED,?1)?==?1)?{??
  • ????????final?String?soundPath?=?Settings.System.getString(cr,?Settings.System.LOW_BATTERY_SOUND);??
  • ????????if?(soundPath?!=?null)?{??
  • ????????????final?Uri?soundUri?=?Uri.parse("file://"?+?soundPath);??
  • ????????????if?(soundUri?!=?null)?{??
  • ????????????????final?Ringtone?sfx?=?RingtoneManager.getRingtone(mContext,?soundUri);??
  • ????????????????if?(sfx?!=?null)?{??
  • ????????????????????sfx.setStreamType(AudioManager.STREAM_SYSTEM);??
  • ????????????????????sfx.play();??
  • ????????????????}??
  • ????????????}??
  • ????????}??
  • ????}??
  • }??

  • 總結

    以上是生活随笔為你收集整理的Android Low Battery 低电量处理流程的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。