Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect power connected state?

Tags:

android

Is there an easy way to be notified when USB or AC power is connected to an Android phone?

like image 349
NPike Avatar asked Jul 16 '10 16:07

NPike


4 Answers

In AndroidManifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <receiver android:name=".receiver.PlugInControlReceiver">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
        </intent-filter>
    </receiver>
</application>

In Code

public class PlugInControlReceiver extends BroadcastReceiver {
   public void onReceive(Context context , Intent intent) {
       String action = intent.getAction();

       if(action.equals(Intent.ACTION_POWER_CONNECTED)) {
           // Do something when power connected
       }
       else if(action.equals(Intent.ACTION_POWER_DISCONNECTED)) {
           // Do something when power disconnected
       }
   }
}
like image 180
J.J. Kim Avatar answered Nov 07 '22 02:11

J.J. Kim


Set up a BroadcastReceiver for ACTION_BATTERY_CHANGED. An Intent extra will tell you what the charging state is -- see BatteryManager for details.

like image 31
CommonsWare Avatar answered Nov 07 '22 01:11

CommonsWare


another way is using battery manager. this i usable for api>=21

public class PowerUtil {
    public static boolean isConnected(Context context) {
        Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
    }
}
like image 42
iman kazemayni Avatar answered Nov 07 '22 00:11

iman kazemayni


Here is another way to poll the information:

read the values here:ex.

via android shell:

cat /sys/class/power_supply/usb/online

1=connected, 0=not. Reflects USB connection status.

cat /sys/class/power_supply/ac/online

1=connected, 0=not. Reflects AC connection status.

Using both of these together, I think will tell whether the device is receiving power or not. Not sure if the location is the same for all devices. Found the location the same though on android 7+ and 5+, on a Samsung tablet and a RockChip device.

For the devices I mentioned tested, it worked. Files are RO, read only, you would only read them to poll the information. The android API's did not provide the level of detail I needed at the version I was required to use (5.1.1), this did. I used provided android API to create a process run those commands. It doesn't require root. This was done for an kiosk app. You can also run the same process using only android API (file, FileReader, etc).

Here is an Android API example:

File aFile = new File("/sys/class/power_supply/ac/online");
try {
    BufferedReader br = new BufferedReader(new FileReader(aFile));
    char aBuff[] = new char[1];
    int aCount = br.read(aBuff,0, 1);
    Log.d(TAG, "run: Res:"+new String(aBuff));
}catch(Exception e){
    Log.d(TAG, "Exception:Error:run: "+e.toString());
}
like image 4
Jesse Palmer Avatar answered Nov 07 '22 01:11

Jesse Palmer