在广播接收器响应Intent.ACTION_BATTERY_CHANGED之前获得电池电量

时间:2010-09-07 18:21:48

标签: android broadcastreceiver battery

我的程序中有一个广播接收器,可以像这样对电池电量做出反应:

private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context arg0, Intent intent) {
        int level = intent.getIntExtra("level", 0);
        // do something...
    }
}

    registerReceiver(this.mBatInfoReceiver, 
            new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

但是,此代码必须等待更新电池状态,因此如果您需要根据电池电量设置GUI元素,则必须等待电池事件发生。有没有办法轻推这个以使其工作或只是运行一些代码来查看最后一次广播的电池电量是多少?

6 个答案:

答案 0 :(得分:83)

这是如何在不注册接收器的情况下获得电池电量:

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;
}

由于广播的粘性,它可以使用null BroadcastReceiver。

它使用getApplicationContext()技巧,以防你在intent接收器中并获得异常:

  

android.content.ReceiverCallNotAllowedException:   IntentReceiver组件不是   允许注册接收   意图

答案 1 :(得分:37)

  

有没有办法轻推它以使其正常工作或只是运行一些代码来查看电池电量是多少   最后一次播出?

您可以使用registerReceiver()IntentFilter null致电BroadcastReceiver以获取最后广播Intent。这是有效的,因为ACTION_BATTERY_CHANGED是所谓的“粘性广播”,我在this StackOverflow question-and-answer中对此进行了更多描述。

答案 2 :(得分:5)

    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) + "%";
    }

答案 3 :(得分:2)

// 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));
}

答案 4 :(得分:1)

我使用此方法获取电池电量而不接收更新。

public float getMyBatteryLevel() {
        Intent batteryIntent = this.getApplicationContext().registerReceiver(null,
        new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        return batteryIntent.getIntExtra("level", -1);
}

答案 5 :(得分:0)

请注意,您从registerReceiver调用的意图

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

可以为null。所以请在使用意图之前进行检查,例如

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

我刚收到空指针异常,导致应用程序崩溃!