Get battery level before broadcast receiver responds for Intent.ACTION_BATTERY_CHANGED

后端 未结 6 1763
囚心锁ツ
囚心锁ツ 2020-11-28 03:56

I have a broadcast receiver in my program to get react to the battery level like so:

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
           


        
相关标签:
6条回答
  • 2020-11-28 04:01

    Is there a way to nudge this to get it working or simply run some code to see what the battery level was on the last broadcast?

    You can call registerReceiver() with your IntentFilter and a null BroadcastReceiver to get the last-broadcast Intent. This works because ACTION_BATTERY_CHANGED is a so-called "sticky broadcast", which I describe a bit more in this StackOverflow question-and-answer.

    0 讨论(0)
  • 2020-11-28 04:04

    Please be aware, that the intent you get from registerReceiver call

    Intent intent  = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    

    can be null. So please make a check before using the intent, e.g.

    if(intent != null){ 
    // do your stuff here... 
    }
    

    I just got a null pointer exception, causing the app to crash!

    0 讨论(0)
  • 2020-11-28 04:11

    This is how to get the battery level without registering a receiver:

    Intent batteryIntent = context.getApplicationContext().registerReceiver(null,
                        new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int rawlevel = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    double scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
    double level = -1;
    if (rawlevel >= 0 && scale > 0) {
        level = rawlevel / scale;
    }
    

    It can use a null BroadcastReceiver because of the sticky nature of the broadcast.

    It uses the getApplicationContext() trick in case you are in a intent receiver and get the exception:

    android.content.ReceiverCallNotAllowedException: IntentReceiver components are not allowed to register to receive intents

    0 讨论(0)
  • 2020-11-28 04:16
        public static String batteryLevel(Context context)
        {
            Intent intent  = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));   
            int    level   = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
            int    scale   = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
            int    percent = (level*100)/scale;
            return String.valueOf(percent) + "%";
        }
    
    0 讨论(0)
  • I use this method to get the battery level without receiving updates.

    public float getMyBatteryLevel() {
            Intent batteryIntent = this.getApplicationContext().registerReceiver(null,
            new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
            return batteryIntent.getIntExtra("level", -1);
    }
    
    0 讨论(0)
  • 2020-11-28 04:27
    // Put this Code into your MainActivity
    
    private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context c, Intent i) {
            int level = i.getIntExtra("level", 0);
            ProgressBar pb = (ProgressBar) findViewById(R.id.progressbar);
            pb.setProgress(level);
            TextView tv = (TextView) findViewById(R.id.textfield);
            tv.setText("Battery Level: " + Integer.toString(level) + "%");
        }
    
    };
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        registerReceiver(mBatInfoReceiver, new IntentFilter(
                Intent.ACTION_BATTERY_CHANGED));
    }
    
    0 讨论(0)
提交回复
热议问题