0
public void batteryStatusChange(int status) 
{
    if(DeviceInfo.getBatteryLevel() == 70)
    {
// TODO Auto-generated method stub
        //play a tune that tells the user that yourbattery is at 70%

    }
}

查看文档,状态为“来自 DeviceInfo 的 BSTAT_xxx 掩码的组合”

如果我的电池电量百分比下降,例如71%到70%,即使我不使用status参数,这个函数是否会被SystemListener2接口调用?

如果我想在 BSTAT 中更具体一些,说只在电池电量发生变化时激活函数内部的方法,而不是在检测到任何类型的变化(如热或冷)时激活,然后这段代码:

public void batteryStatusChange(int status) 
    {
        if(status == DeviceInfo.BSTAT_LEVEL_CHANGED)
            if(DeviceInfo.getBatteryLevel() == 70)
                {
            //play a tune that tells the user that ur battery is at 70%
        // TODO Auto-generated method stub      
                }
    }

是基本上和第一个代码一样,但是检查到水平变化?

4

1 回答 1

3

如果status掩码的组合,那么我认为您需要进行此测试,以确定该值是否包含该位。BSTAT_ intBSTAT_LEVEL_CHANGED

public void batteryStatusChange(int status) 
{
    if ((status & DeviceInfo.BSTAT_LEVEL_CHANGED) != 0)
    {
        if(DeviceInfo.getBatteryLevel() == 70)
        {
            //play a tune that tells the user that ur battery is at 70%
        }
    }
}

或者,我想另一种自己跟踪的方法就是将级别记录为成员变量:

private int currentBatteryLevel = -1;

public void batteryStatusChange(int status) 
{
    int newBatteryLevel = DeviceInfo.getBatteryLevel();
    if (currentBatteryLevel != newBatteryLevel) 
    {
        currentBatteryLevel = newBatteryLevel;
        if(DeviceInfo.getBatteryLevel() == 70)
        {
            //play a tune that tells the user that ur battery is at 70%
        }
    }
}
于 2012-09-17T05:55:00.880 回答