@Override public void onReceive(final Context context, final Intent intent) { String action = intent.getAction(); if (Intent.ACTION_POWER_CONNECTED.equals(action) || Intent.ACTION_POWER_DISCONNECTED.equals(action)) { // バッテリーが充電された時 mHostBatteryManager.setBatteryRequest(intent); List<Event> events = EventManager.INSTANCE.getEventList( mServiceId, HostBatteryProfile.PROFILE_NAME, null, HostBatteryProfile.ATTRIBUTE_ON_CHARGING_CHANGE); for (int i = 0; i < events.size(); i++) { Event event = events.get(i); Intent mIntent = EventManager.createEventMessage(event); HostBatteryProfile.setAttribute( mIntent, HostBatteryProfile.ATTRIBUTE_ON_CHARGING_CHANGE); Bundle charging = new Bundle(); if (Intent.ACTION_POWER_CONNECTED.equals(action)) { HostBatteryProfile.setCharging(charging, true); } else { HostBatteryProfile.setCharging(charging, false); } HostBatteryProfile.setBattery(mIntent, charging); sendEvent(mIntent, event.getAccessToken()); } } }
@Override public void onReceive(final Context context, final Intent intent) { String action = intent.getAction(); if (Intent.ACTION_BATTERY_CHANGED.equals(action) || Intent.ACTION_BATTERY_LOW.equals(action) || Intent.ACTION_BATTERY_OKAY.equals(action)) { // バッテリーが変化した時 mHostBatteryManager.setBatteryRequest(intent); List<Event> events = EventManager.INSTANCE.getEventList( mServiceId, HostBatteryProfile.PROFILE_NAME, null, HostBatteryProfile.ATTRIBUTE_ON_BATTERY_CHANGE); for (int i = 0; i < events.size(); i++) { Event event = events.get(i); Intent mIntent = EventManager.createEventMessage(event); HostBatteryProfile.setAttribute( mIntent, HostBatteryProfile.ATTRIBUTE_ON_BATTERY_CHANGE); Bundle battery = new Bundle(); double level = ((double) (mHostBatteryManager.getBatteryLevel())) / ((double) getBatteryScale()); HostBatteryProfile.setLevel(battery, level); HostBatteryProfile.setBattery(mIntent, battery); sendEvent(mIntent, event.getAccessToken()); } } }