电池状态设置为满时如何获取信息
How to get information when battery status sets full
我的问题是...当电池状态设置为充满时如何获取信息。
我已经尝试通过 ACTION_BATTERY_CHANGED 上的接收器执行此操作,但这不起作用,我几乎可以肯定接收器甚至没有收到此广播。也许我没有正确理解这一点,action_battery_changed 意味着当我们的电池得到另一个 % 时发出信号?或者仅在断开连接或连接等特定事件中,因为此操作在我的应用程序中有效(在另一个 class 接收器中)。
<receiver android:name="com.lily.chargingscheduler.ChangingBatteryStatsOnReceiver" >
<intent-filter>
<action android:name="android.intent.action.ACTION_BATTERY_CHANGED" />
</intent-filter>
</receiver>
和class:
public class ChangingBatteryStatsOnReceiver extends BroadcastReceiver {
static BDHelper db_helper;
private String data;
String level_;
String answer;
@Override
public void onReceive(Context context, Intent intent) {
db_helper = new BDHelper(context);
data = (new SimpleDateFormat("MM/dd/yyyy HH:mm")).format(new Date());
level_ = batteryLevel(context);
if(db_helper.getLastRow().get_TO_D().equals("")){
if(level_ == "100%"){// level return % as a String
answer = calculate();//calculate returns date and other things
ending_charging(context);//adding to table and notification of this event
}
}
}
[编辑]
也许我不够具体,我在获取有关电池电量的信息时没有问题,我在电池充满后获取信息时遇到问题。
public void onReceive(Context context, Intent intent) {
context.unregisterReceiver(this);
int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int level = -1;
if (currentLevel >= 0 && scale > 0) {
level = (currentLevel * 100) / scale; // level will returns 99 or 100 when battery is full
}
batteryPercent.setText("Battery Level Remaining: " + level + "%");
}
};
IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}
另外,如果您想在低电量状态下收听电池,请添加此 <intent-filter/>
:
<receiver android:name=".ChangingBatteryStatsOnReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_BATTERY_LOW"/>
<action android:name="android.intent.action.ACTION_BATTERY_OKAY"/>
</intent-filter>
</receiver>
我的问题是...当电池状态设置为充满时如何获取信息。 我已经尝试通过 ACTION_BATTERY_CHANGED 上的接收器执行此操作,但这不起作用,我几乎可以肯定接收器甚至没有收到此广播。也许我没有正确理解这一点,action_battery_changed 意味着当我们的电池得到另一个 % 时发出信号?或者仅在断开连接或连接等特定事件中,因为此操作在我的应用程序中有效(在另一个 class 接收器中)。
<receiver android:name="com.lily.chargingscheduler.ChangingBatteryStatsOnReceiver" >
<intent-filter>
<action android:name="android.intent.action.ACTION_BATTERY_CHANGED" />
</intent-filter>
</receiver>
和class:
public class ChangingBatteryStatsOnReceiver extends BroadcastReceiver {
static BDHelper db_helper;
private String data;
String level_;
String answer;
@Override
public void onReceive(Context context, Intent intent) {
db_helper = new BDHelper(context);
data = (new SimpleDateFormat("MM/dd/yyyy HH:mm")).format(new Date());
level_ = batteryLevel(context);
if(db_helper.getLastRow().get_TO_D().equals("")){
if(level_ == "100%"){// level return % as a String
answer = calculate();//calculate returns date and other things
ending_charging(context);//adding to table and notification of this event
}
}
}
[编辑]
也许我不够具体,我在获取有关电池电量的信息时没有问题,我在电池充满后获取信息时遇到问题。
public void onReceive(Context context, Intent intent) {
context.unregisterReceiver(this);
int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int level = -1;
if (currentLevel >= 0 && scale > 0) {
level = (currentLevel * 100) / scale; // level will returns 99 or 100 when battery is full
}
batteryPercent.setText("Battery Level Remaining: " + level + "%");
}
};
IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}
另外,如果您想在低电量状态下收听电池,请添加此 <intent-filter/>
:
<receiver android:name=".ChangingBatteryStatsOnReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_BATTERY_LOW"/>
<action android:name="android.intent.action.ACTION_BATTERY_OKAY"/>
</intent-filter>
</receiver>