设置充电器的时间连接和断开[android]

时间:2014-05-14 21:25:17

标签: android broadcastreceiver

我想创建控制手机与充电器连接的应用程序。应用程序应记住连接和断开充电器的时间。 要使用共享首选项存储数据。我正在使用广播接收器来控制设备何时充电。

这是我的广播:

public class PowerConnectionReceiver extends BroadcastReceiver {

        SharedPreferences pref;

        public void onReceive(Context context, Intent intent) { 

            pref = context.getSharedPreferences("StatusOfCharging",Activity.MODE_PRIVATE);
            SharedPreferences.Editor prefEditor = pref.edit();

                int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);         
                boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                        status == BatteryManager.BATTERY_STATUS_FULL;

                prefEditor.putBoolean("isCharging", false).commit();

                if (isCharging == true){
                    Log.d("daf", "work");

                }else{
                    prefEditor.putBoolean("isCharging", false).commit();

                }

                int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);

                boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
                boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

        }
}

有人知道如何记住充电时间吗?

0 个答案:

没有答案