Example usage for android.content Intent ACTION_BATTERY_CHANGED

List of usage examples for android.content Intent ACTION_BATTERY_CHANGED

Introduction

In this page you can find the example usage for android.content Intent ACTION_BATTERY_CHANGED.

Prototype

String ACTION_BATTERY_CHANGED

To view the source code for android.content Intent ACTION_BATTERY_CHANGED.

Click Source Link

Document

Broadcast Action: This is a sticky broadcast containing the charging state, level, and other information about the battery.

Usage

From source file:org.span.manager.MainActivity.java

@Override
public void onResume() {
    super.onResume();
    Log.d(TAG, "onResume()"); // DEBUG

    // check if the battery temperature should be displayed
    if (app.prefs.getString("batterytemppref", "fahrenheit").equals("disabled") == false) {
        // create the IntentFilter that will be used to listen
        // to battery status broadcasts
        intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(intentReceiver, intentFilter);
        batteryTemperatureLayout.setVisibility(View.VISIBLE);
    } else {//w  w  w.  j a va  2  s  .  c  o  m
        try {
            unregisterReceiver(this.intentReceiver);
        } catch (Exception e) {
            ;
        }
        batteryTemperatureLayout.setVisibility(View.INVISIBLE);
    }

    // Register to receive updates about the device network state
    registerReceiver(intentReceiver, new IntentFilter(WifiManager.NETWORK_STATE_CHANGED_ACTION));
    registerReceiver(intentReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

    /*
      Window window = getWindow();
      // window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
      // window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
      window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
      window.addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
      */
}

From source file:vn.cybersoft.obs.android.fragments.BatteryInfoFragment.java

@Override
public void onResume() {
    super.onResume();
    Log.v(t + ".onResume()");
    getActivity().registerReceiver(mBatteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}

From source file:org.deviceconnect.android.deviceplugin.host.HostDeviceService.java

@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {

    if (intent == null) {
        return START_STICKY;
    }//  w  ww . ja v a  2 s.  c o  m

    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(mDeviceId, 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();
            HostBatteryProfile.setLevel(battery, mHostBatteryManager.getBatteryLevel());
            getContext().sendBroadcast(mIntent);
        }

        return START_STICKY;
    } else if (Intent.ACTION_POWER_CONNECTED.equals(action)
            || Intent.ACTION_POWER_DISCONNECTED.equals(action)) {
        // ????

        mHostBatteryManager.setBatteryRequest(intent);

        List<Event> events = EventManager.INSTANCE.getEventList(mDeviceId, 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);
            getContext().sendBroadcast(mIntent);
        }

        return START_STICKY;
    } else if (action.equals("android.intent.action.NEW_OUTGOING_CALL")) {
        // Phone

        List<Event> events = EventManager.INSTANCE.getEventList(mDeviceId, HostPhoneProfile.PROFILE_NAME, null,
                HostPhoneProfile.ATTRIBUTE_ON_CONNECT);

        for (int i = 0; i < events.size(); i++) {
            Event event = events.get(i);
            Intent mIntent = EventManager.createEventMessage(event);

            HostPhoneProfile.setAttribute(mIntent, HostPhoneProfile.ATTRIBUTE_ON_CONNECT);
            Bundle phoneStatus = new Bundle();
            HostPhoneProfile.setPhoneNumber(phoneStatus, intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER));
            HostPhoneProfile.setState(phoneStatus, CallState.START);
            HostPhoneProfile.setPhoneStatus(mIntent, phoneStatus);
            getContext().sendBroadcast(mIntent);
        }

        return START_STICKY;
    } else if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)
            || WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) {
        // Wifi

        List<Event> events = EventManager.INSTANCE.getEventList(mDeviceId, HostConnectProfile.PROFILE_NAME,
                null, HostConnectProfile.ATTRIBUTE_ON_WIFI_CHANGE);

        for (int i = 0; i < events.size(); i++) {
            Event event = events.get(i);
            Intent mIntent = EventManager.createEventMessage(event);

            HostConnectProfile.setAttribute(mIntent, HostConnectProfile.ATTRIBUTE_ON_WIFI_CHANGE);
            Bundle wifiConnecting = new Bundle();
            WifiManager wifiMgr = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
            HostConnectProfile.setEnable(wifiConnecting, wifiMgr.isWifiEnabled());
            HostConnectProfile.setConnectStatus(mIntent, wifiConnecting);
            getContext().sendBroadcast(mIntent);
        }

        return START_STICKY;

    } else if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {

        List<Event> events = EventManager.INSTANCE.getEventList(mDeviceId, HostConnectProfile.PROFILE_NAME,
                null, HostConnectProfile.ATTRIBUTE_ON_BLUETOOTH_CHANGE);

        for (int i = 0; i < events.size(); i++) {
            Event event = events.get(i);
            Intent mIntent = EventManager.createEventMessage(event);

            HostConnectProfile.setAttribute(mIntent, HostConnectProfile.ATTRIBUTE_ON_BLUETOOTH_CHANGE);
            Bundle bluetoothConnecting = new Bundle();
            BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
            HostConnectProfile.setEnable(bluetoothConnecting, mBluetoothAdapter.isEnabled());
            HostConnectProfile.setConnectStatus(mIntent, bluetoothConnecting);
            getContext().sendBroadcast(mIntent);
        }

        return START_STICKY;
    }
    return super.onStartCommand(intent, flags, startId);
}

From source file:org.proninyaroslav.libretorrent.core.utils.Utils.java

public static boolean isBatteryCharging(Context context) {
    Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);

    return status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
}

From source file:system.info.reader.java

@Override
protected void onResume() {
    super.onResume();
    //register to receive battery intent
    Properties.BatteryString = (String) propertyItems[0];
    Properties.batteryHealth = getResources().getTextArray(R.array.batteryHealthState);
    Properties.batteryStatus = getResources().getTextArray(R.array.batteryStatus);
    IntentFilter filter = new IntentFilter();
    filter.addAction(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(Properties.BroadcastReceiver, filter);
}

From source file:com.acrr.acdisplay.services.activemode.ActiveModeService.java

@Override
public void onStart(Object... objects) {
    if (DEBUG)/*from w ww  .j a  v  a2s . co m*/
        Log.d(TAG, "Starting listening to sensors.");

    Context context = getContext();
    SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
    for (ActiveModeSensor sensor : mSensors) {
        sensor.registerCallback(this);
        sensor.onAttached(sensorManager, context);
    }

    mActiveChargingEnabled = Config.getInstance().isActiveModeActiveChargingEnabled();
    if (mActiveChargingEnabled) {
        mPluggedAtomic.react(PowerUtils.isPlugged(context));

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
        context.registerReceiver(mReceiver, intentFilter);
    }
}

From source file:com.google.transporttracker.TrackerService.java

private float getBatteryLevel() {
    Intent batteryStatus = registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int batteryLevel = -1;
    int batteryScale = 1;
    if (batteryStatus != null) {
        batteryLevel = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, batteryLevel);
        batteryScale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, batteryScale);
    }/*from w ww  . ja  va  2s  .co m*/
    return batteryLevel / (float) batteryScale * 100;
}

From source file:syncthing.android.service.ServiceSettings.java

boolean isCharging() {
    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent battChanged = appContext.registerReceiver(null, filter);//stickies returned by register
    int status = battChanged != null ? battChanged.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0) : 0;
    return status != 0;
}

From source file:org.protocoderrunner.apprunner.api.PDevice.java

@ProtoMethod(description = "", example = "")
@ProtoMethodParam(params = { "" })
public void battery(final StartBateryListenerCB cb) {
    WhatIsRunning.getInstance().add(this);
    batteryReceiver = new BroadcastReceiver() {
        int scale = -1;
        int level = -1;
        int voltage = -1;
        int temp = -1;
        boolean isConnected = false;
        private int status;
        private final boolean alreadyKilled = false;

        @Override/*from  ww  w .  java2s. c o m*/
        public void onReceive(Context context, Intent intent) {
            level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
            voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
            // isCharging =
            // intent.getBooleanExtra(BatteryManager.EXTRA_PLUGGED, false);
            // status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
            status = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);

            if (status == BatteryManager.BATTERY_PLUGGED_AC) {
                isConnected = true;
            } else if (status == BatteryManager.BATTERY_PLUGGED_USB) {
                isConnected = true;
            } else {
                isConnected = false;
            }

            BatteryReturn o = new BatteryReturn();

            o.level = level;
            o.temperature = temp;
            o.connected = isConnected;

            // plugConnected = isConnected;
            cb.event(o);
            Log.d("BATTERY", "level is " + level + " is connected " + isConnected);
        }
    };

    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    getContext().registerReceiver(batteryReceiver, filter);
}

From source file:com.justwayward.reader.ui.activity.ReadActivity.java

@Override
public void initDatas() {
    recommendBooks = (Recommend.RecommendBooks) getIntent().getSerializableExtra(INTENT_BEAN);
    bookId = recommendBooks._id;//from ww w.j  ava  2s.c  o  m
    isFromSD = getIntent().getBooleanExtra(INTENT_SD, false);

    if (Intent.ACTION_VIEW.equals(getIntent().getAction())) {
        String filePath = Uri.decode(getIntent().getDataString().replace("file://", ""));
        String fileName;
        if (filePath.lastIndexOf(".") > filePath.lastIndexOf("/")) {
            fileName = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.lastIndexOf("."));
        } else {
            fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
        }

        CollectionsManager.getInstance().remove(fileName);
        // 
        File desc = FileUtils.createWifiTranfesFile(fileName);
        FileUtils.fileChannelCopy(new File(filePath), desc);
        // 
        recommendBooks = new Recommend.RecommendBooks();
        recommendBooks.isFromSD = true;
        recommendBooks._id = fileName;
        recommendBooks.title = fileName;

        isFromSD = true;
    }
    EventBus.getDefault().register(this);
    showDialog();

    mTvBookReadTocTitle.setText(recommendBooks.title);

    mTtsPlayer = TTSPlayerUtils.getTTSPlayer();
    ttsConfig = TTSPlayerUtils.getTtsConfig();

    intentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
    intentFilter.addAction(Intent.ACTION_TIME_TICK);

    CollectionsManager.getInstance().setRecentReadingTime(bookId);
    Observable.timer(1000, TimeUnit.MILLISECONDS).subscribe(new Action1<Long>() {
        @Override
        public void call(Long aLong) {
            //1
            EventManager.refreshCollectionList();
        }
    });
}