Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android低電量處理流程

android低電量處理流程

編輯:關於Android編程

前一段時間分析的一個小過程,把它記下來:

我們都知道android電量的一些功能很多在service中,低電量也是如此,在BatteryService.java中我們可以從jni層(當然jni層的電量也是從下層kernel接收而來,這裡就不做過多分析,有興趣的可以去深入了解一下)獲得機器的電量mBatteryLevel,我們也知道當手機電量低的時候它會有警告有些還發出聲音提醒你,並且太低時會自動關機!
下面簡單介紹一下其流程:

在BatteryService.java中,當我們從jni獲得當前電量之後,可以在 update()中做一個判斷:
 final boolean sendBatteryLow = !plugged

                && mBatteryStatus != BatteryManager.BATTERY_STATUS_UNKNOWN

                && mBatteryLevel <= mLowBatteryWarningLevel

                && (oldPlugged || mLastBatteryLevel > mLowBatteryWarningLevel);

           
如果當前電量小於警告電量(在config.xml中 <integer name="config_lowBatteryWarningLevel">15</integer>)則彈出電量低提示,或者電量為0(當然這個有誤差也可能是5%時就自動關機)時自動關機。

在下面會有判斷是否低電量:

if (sendBatteryLow) {

                mSentLowBatteryBroadcast = true;

                statusIntent.setAction(Intent.ACTION_BATTERY_LOW);

                mContext.sendBroadcast(statusIntent);
      }
如果低電量的話就發送一個廣播出去。
這段代碼是電量太低而自動關機:
private final void shutdownIfNoPower() {
     
        if (mBatteryLevel == 0 && !isPowered() && 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.startActivity(intent);

        }

    }

而在StatusBarPolicy.java中會有接收廣播:private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
               。。。。。。。。。

裡面會有判斷當接受到ACTION_BATTERY_LOW時:

else if (action.equals(Intent.ACTION_BATTERY_LOW)) {

                onBatteryLow(intent);

            }
在StatusBarPolicy.java中會有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了:
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() {
                        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) {  www.2cto.com
                        sfx.setStreamType(AudioManager.STREAM_SYSTEM);
                        sfx.play();
                    }
                }
            }
        }
    }
   //waring voiced這行下面就是低電量提醒的聲音.
   大概過程就是這樣了!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved