42

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

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 元素,则它必须等待电池事件发生。有没有办法推动它使其正常工作或简单地运行一些代码来查看上次广播时的电池电量?

4

6 回答 6

84

这是在不注册接收器的情况下获取电池电量的方法:

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() 技巧,以防您在意图接收器中并获得异常:

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

于 2010-10-28T22:57:13.743 回答
37

有没有办法推动它使其正常工作或简单地运行一些代码来查看上次广播时的电池电量?

你可以registerReceiver()用你的IntentFilter和 a打电话null BroadcastReceiver来获得最后的广播Intent。这是有效的,因为ACTION_BATTERY_CHANGED它是所谓的“粘性广播”,我在StackOverflow question-and-answer中对其进行了更多描述。

于 2010-09-07T19:19:01.907 回答
6
    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) + "%";
    }
于 2014-05-12T16:31:11.130 回答
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));
}
于 2013-04-23T10:14:33.200 回答
1

我使用这种方法在不接收更新的情况下获取电池电量。

public float getMyBatteryLevel() {
        Intent batteryIntent = this.getApplicationContext().registerReceiver(null,
        new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        return batteryIntent.getIntExtra("level", -1);
}
于 2014-02-04T19:09:55.107 回答
0

请注意,您从 registerReceiver 调用中获得的意图

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

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

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

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

于 2016-08-24T12:46:20.810 回答