Android power save mode listener?

后端 未结 3 335
执笔经年
执笔经年 2021-01-12 11:04

How can an Android listener be created to perform a task just before entering power save mode? Also: what are some of the low power options that can be controlled by this ta

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-12 11:35

    The answer above(for API 21 and above) is not exactly right. You should register a receiver in your activity or service like this:

    BroadcastReceiver powerSaverChangeReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            checkPowerSaverMode();
        }
     };
    
    IntentFilter filter = new IntentFilter();
    filter.addAction("android.os.action.POWER_SAVE_MODE_CHANGED");
    registerReceiver(powerSaverChangeReceiver, filter);
    

    The reason for this is that receivers in manifest are not triggered by this broadcast.

    ACTION_POWER_SAVE_MODE_CHANGED

    Intent that is broadcast when the state of isPowerSaveMode() changes. This broadcast is only sent to registered receivers.

    I have tested this and it works.

提交回复
热议问题