安卓:使用报警管理器获取电池状态定期 [英] Android: Get Battery Status at regular intervals using alarm manager

查看:178
本文介绍了安卓:使用报警管理器获取电池状态定期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得Android设备的电池状态定期。

I want to get the battery status of an Android device at regular intervals.

由于这是一个坏主意,轮询电池将每两分钟,我想用一个重复报警,即熄灭,也许一小时一次,调用类,获取当前的电池状态。

As it is a bad idea to poll for battery changes every couple of minutes, I wanted to use a repeating alarm, which goes off maybe once an hour, to call a class which gets the current battery status.

不过我目前的执行情况是给我一个例外:

However my current implementation is giving me an exception:

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

类被称为当报警超时如下:

The class which is called when the alarm times out is as follows:

public class GetBatteryStatusTimeout extends BroadcastReceiver{
    private static final String TAG = "GetBatteryStatusTimeout";
    @Override
    public void onReceive(Context context, Intent intent) {
        BatteryDataManager batteryData = new BatteryDataManager(context);
        batteryData.getStatus();
        Log.e(TAG, "GetBatteryStatus timer expired, getting status");
    }
}

的getStatus()方法调用code登记为粘稠的意图,然后将电池状态:

The getStatus() method calls the code to register for the sticky intent and get the battery status:

IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING;
boolean fullyCharged = status == BatteryManager.BATTERY_STATUS_FULL;
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

这是我注册这个接收器的异常被抛出点。

It is the point at which I register for this receiver that the exception is thrown.

我明白什么是异常是说(我想!),但我不调用code登记为粘性意图从另一个接收器?

I understand what the exception is saying (I think!) but I am not calling the code to register for the sticky intent from within another receiver?

报警接收器调用另一个类,然后注册接收器的电池变的意图....但显然这是不允许的要么?

The alarm receiver calls another class which then registers the receiver for the battery changed intents.... but obviously this isn't allowed either?

什么是另类?

另外,除了只抛出了一些设备,以及应用程序正常工作的人?

Also the exception is only thrown on some devices, and the app works fine on others?

推荐答案

这其实是一个Android的bug。你是不是真正想注册的BroadcastReceiver 在这种情况下,异步回调,但无论如何,Android的抛出该异常。

This is actually an Android bug. You aren't actually trying to register a BroadcastReceiver for an asynchronous callback in this case, but Android throws this exception anyway.

要解决这个问题,而不是调用

To get around this, instead of calling

context.registerReceiver(null, ifilter);

要获得持久广播,做到这一点:

to get the sticky broadcast, do this:

context.getApplicationContext().registerReceiver(null, ifilter);

应用程序上下文有更长的寿命比接收器的上下文,所以通话将被允许的。

The application Context has a longer lifetime than the receiver's Context, so the call will be allowed.

这篇关于安卓:使用报警管理器获取电池状态定期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆