我想通过应用程序在电池充电(插入电源)时显示通知灯。获得电池状态不是问题,但打开灯对我来说真的很难。我希望手机显示红灯,如果电池电量低于15%,则橙色至89%,然后当电池电量达到90%-100%时绿灯亮。
到目前为止,我使用广播接收器在用户插件或断开电池充电时获得充电状态。
广播接收器
public class Battery_Indicator_Broadcast extends BroadcastReceiver {
public static final String TAG="###Battery Status###";
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG,"Battery Intent "+intent);
int status=intent.getIntExtra(BatteryManager.EXTRA_STATUS,-1);
boolean isCharging=status==BatteryManager.BATTERY_STATUS_CHARGING ||
status==BatteryManager.BATTERY_STATUS_FULL;
Log.d(TAG,"Battery Status FUll or Charging "+ BatteryManager.BATTERY_STATUS_CHARGING+" "+BatteryManager.BATTERY_STATUS_FULL+" "+isCharging);
if (isCharging){
// Here i need to turn notification light on
}
}
}
清单文件
<receiver android:name=".Battery_Indicator_Broadcast">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED"></action>
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED"></action>
<action android:name="android.intent.action.BATTERY_LOW"></action>
<action android:name="android.intent.action.BATTERY_OKAY"></action>
<action android:name="android.intent.action.HEADSET_PLUG"></action>
</intent-filter>
</receiver>