Android : Do something when battery is at a defined level

前端 未结 2 1788
北海茫月
北海茫月 2021-02-06 17:18

I\'m stuck with a little problem here.

I want my app to do something, but only when the battery is at 10%.

My app doesn\'t watch the battery level constantly;

2条回答
  •  离开以前
    2021-02-06 17:31

    In this case, you can use "ACTION_BATTERY_CHANGED"

        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
                int level = intent.getIntExtra("level", 0);
                int scale = intent.getIntExtra("scale", 100);
                double batStatus = level * 100 / scale;
            }
        }
    

    Hope this will help you.

提交回复
热议问题