3

I was wondering if it was possible to specifically determine if an Android device was plugged into a computer or just power.

The main reason is I'd like for my phone to "Stay Awake" when it's plugged into a computer while I'm developing, but not when it's plugged into a power outlet a night.

I was thinking that if I could tell what I'm connected to, I could use ACTION_POWER_CONNECTED to start up a service and check what USB I'm connected to.

Grantland Chew
  • 2,620
  • 26
  • 26
  • 1
    Wow, this is brilliant. That annoys me so hard! Did you take a look at [BATTERY_PLUGGED_USB](http://developer.android.com/reference/android/os/BatteryManager.html#BATTERY_PLUGGED_USB)? – Knickedi Oct 03 '11 at 22:52
  • 1
    Duplicate of http://stackoverflow.com/questions/5547359/battery-charging-notification. That question has your answer. – Steve Blackwell Oct 03 '11 at 22:57
  • 1
    There's an [app](https://market.android.com/details?id=net.leppoc.android.dashboard) which does that already. Thanks for the tip. – Knickedi Oct 03 '11 at 23:15

3 Answers3

4

First you need to register a receiver to listen to Intent.ACTION_BATTERY_CHANGED and check for the BatteryManager.BATTERY_PLUGGED_USB. Below is the code to do so:


BroadcastReceiver receiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        if (plugged == BatteryManager.BATTERY_PLUGGED_USB) {
            Toast.makeText(getApplicationContext(), "Connected to USB, Stay Awake", Toast.LENGTH_LONG).show();
            PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
            PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "WakeLock");
            wl.acquire();
        } 
    }
};

// register the receiver to listen to battery change
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(receiver, filter);

You also need the permission in your AndroidManifest.xml to enable staying awake

<uses-permission android:name="android.permission.WAKE_LOCK" /> 

One other note, at some point you will need to unregister the receiver. You could provide that via settings page to call unregisterReceiver

momo
  • 21,233
  • 8
  • 39
  • 38
1

I actually found the exact way to choose whether to keep the screen on when plugged into AC, USB or either by looking into the source at http://google.com/codesearch#409TP6F96yI/src/com/android/settings/DevelopmentSettings.java&l=95

Requires permission: android.permission.WRITE_SETTINGS

Settings.System.putInt(getContentResolver(), Settings.System.STAY_ON_WHILE_PLUGGED_IN,
                mKeepScreenOn.isChecked() ?
                (BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB) : 0);

For AC: BatteryManager.BATTERY_PLUGGED_AC

For USB: BatteryManager.BATTERY_PLUGGED_USB

For Either: (BatteryManager.BATTERY_PLUGGED_AC | BatteryManager.BATTERY_PLUGGED_USB)

Grantland Chew
  • 2,620
  • 26
  • 26
-3

Well, if it's connected to the power supply and you want it to go to sleep just short press the POWER button.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134