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.alpine_toolkit.AlpineToolkitService.java

@Override
public void onCreate() {
    Log.i(LOG_TAG, "onCreate");
    super.onCreate();

    if (m_battery_receiver == null) { // Fixme: ???
        m_battery_receiver = new BatteryReceiver();
        IntentFilter intent_filter = new IntentFilter();
        intent_filter.addAction(Intent.ACTION_BATTERY_CHANGED);
        intent_filter.addAction(Intent.ACTION_POWER_CONNECTED);
        intent_filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
        intent_filter.addAction(Intent.ACTION_BATTERY_LOW);
        intent_filter.addAction(Intent.ACTION_BATTERY_OKAY);
        registerReceiver(m_battery_receiver, intent_filter);
    }// w w  w  . ja va2 s .  com
}

From source file:zarbel.bat.services.BatteryThread.java

public BatteryThread(Context context) {

    //Saving creation context.
    this.applicationContext = context;

    //Getting the user-defined sleep time
    config = PreferenceManager.getDefaultSharedPreferences(context);

    //Getting user selected sleep time by preferences
    sleepTime = Long// w w w  .  j  a v a  2 s .com
            .valueOf(config.getString(context.getResources().getString(R.string.updates_interval), "300000"));

    //Creating intentFilter to get the battery level
    ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);

    //Getting the notification manager
    mNotificationManager = (NotificationManager) this.applicationContext
            .getSystemService(Context.NOTIFICATION_SERVICE);

    createNotification();

}

From source file:org.addhen.smssync.fragments.LogFragment.java

@Override
public void onResume() {
    log("onResume()");
    super.onResume();
    getActivity().registerReceiver(batteryLevelReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    loadLogs();/* w  ww .  j  av a2 s .  c o  m*/
}

From source file:fr.inria.ucn.collectors.SysStateCollector.java

/**
 * /*from  www  . ja v a 2 s  .c o  m*/
 * @param c
 * @param ts
 * @param change
 */
@SuppressLint("NewApi")
public void run(Context c, long ts, boolean change) {
    try {
        JSONObject data = new JSONObject();
        data.put("on_screen_state_change", change); // this collection run was triggered by screen state change

        data.put("hostname", Helpers.getSystemProperty("net.hostname", "unknown hostname"));
        data.put("current_timezone", Time.getCurrentTimezone());

        // general memory state
        ActivityManager am = (ActivityManager) c.getSystemService(Context.ACTIVITY_SERVICE);
        MemoryInfo mi = new MemoryInfo();
        am.getMemoryInfo(mi);

        JSONObject mem = new JSONObject();
        mem.put("available", mi.availMem);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {
            mem.put("total", mi.totalMem);
        }
        mem.put("is_low", mi.lowMemory);
        data.put("memory", mem);

        // screen state
        PowerManager pm = (PowerManager) c.getSystemService(Context.POWER_SERVICE);
        data.put("screen_on", pm.isScreenOn());

        // battery state
        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        Intent battery = c.registerReceiver(null, ifilter);
        int level = battery.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = battery.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        float pct = (float) (100.0 * level) / scale;
        int status = battery.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharging = (status == BatteryManager.BATTERY_STATUS_CHARGING
                || status == BatteryManager.BATTERY_STATUS_FULL);
        int chargePlug = battery.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
        boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

        JSONObject batt = new JSONObject();
        batt.put("level", level);
        batt.put("scale", scale);
        batt.put("pct", pct);
        batt.put("is_charging", isCharging);
        batt.put("usb_charge", usbCharge);
        batt.put("ac_charge", acCharge);
        data.put("battery", batt);

        // some proc stats
        data.put("cpu", getCpuStat());
        data.put("loadavg", getLoadStat());
        data.put("uptime", getUptimeStat());

        // audio state
        data.put("audio", getAudioState(c));

        // done
        Helpers.sendResultObj(c, "system_state", ts, data);

    } catch (JSONException jex) {
        Log.w(Constants.LOGTAG, "failed to create json object", jex);
    }
}

From source file:com.wso2.mobile.mdm.api.PhoneState.java

/**
*Returns the device battery information//from  ww w.jav a2 s.  c om
*/
public float getBatteryLevel() {
    Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

    // Error checking that probably isn't needed but I added just in case.
    if (level == -1 || scale == -1) {
        return 50.0f;
    }

    return ((float) level / (float) scale) * 100.0f;
    /*BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
    int scale = -1;
    int level = -1;
    int voltage = -1;
    int temp = -1;
    @Override
    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);
        Log.v("Battery Level", "level is "+level+"/"+scale+", temp is "+temp+", voltage is "+voltage);
                
        Battery battery = Battery.getInstance();
        battery.setLevel(level);
        battery.setScale(scale);
        battery.setTemp(temp);
        battery.setVoltage(voltage);
                
    }
      };
      IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
      context.registerReceiver(batteryReceiver, filter);
    return Battery.getInstance();*/
}

From source file:org.wso2.cdm.agent.api.PhoneState.java

/**
*Returns the device battery information//from w  w w  . j av a2  s  . co  m
*/
public float getBatteryLevel() {
    Intent batteryIntent = context.getApplicationContext().registerReceiver(null,
            new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
    int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

    // Error checking that probably isn't needed but I added just in case.
    if (level == -1 || scale == -1) {
        return 50.0f;
    }

    return ((float) level / (float) scale) * 100.0f;
    /*BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
    int scale = -1;
    int level = -1;
    int voltage = -1;
    int temp = -1;
    @Override
    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);
        Log.v("Battery Level", "level is "+level+"/"+scale+", temp is "+temp+", voltage is "+voltage);
                
        Battery battery = Battery.getInstance();
        battery.setLevel(level);
        battery.setScale(scale);
        battery.setTemp(temp);
        battery.setVoltage(voltage);
                
    }
      };
      IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
      context.registerReceiver(batteryReceiver, filter);
    return Battery.getInstance();*/
}

From source file:com.sssemil.advancedsettings.MainService.java

public static boolean isPhonePluggedIn(Context context) {
    boolean charging = false;

    final Intent batteryIntent = context.registerReceiver(null,
            new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int status = 0;
    if (batteryIntent != null) {
        status = batteryIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
    }//from w  ww . j a va 2  s  .  c  o m
    boolean batteryCharge = status == BatteryManager.BATTERY_STATUS_CHARGING;

    int chargePlug = 0;
    if (batteryIntent != null) {
        chargePlug = batteryIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
    }
    boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
    boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;

    if (batteryCharge)
        charging = true;
    if (usbCharge)
        charging = true;
    if (acCharge)
        charging = true;

    return charging;
}

From source file:edu.uri.egr.hermes.manipulators.FileLog.java

public String battery() {
    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = Hermes.get().getContext().registerReceiver(null, filter);

    return String.valueOf(batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL,
            -1 / batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1) * 100));
}

From source file:rus.cpuinfo.AndroidDepedentModel.BatteryInfo.java

@Nullable
private Intent getBatteryIntent() {
    return getContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}

From source file:org.addhen.smssync.presentation.view.ui.fragment.LogFragment.java

@Override
public void onResume() {
    super.onResume();
    mListLogPresenter.resume();//from  w  w  w  .  j  a v a2  s  . c  o m
    getActivity().registerReceiver(batteryLevelReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}