[AndroidTips]Check if device is plugged in

My app has a broadcast receiver to listen for changes to ACTION_POWER_CONNECTED, and in turn flag the screen to stay on.

What I am missing is the ability for the app to check the charging status when it first runs. Can anyone please help me with code to manually check charging status?

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;
}
}



  

posted on 2012-03-07 23:15  androidme  阅读(292)  评论(0编辑  收藏  举报

导航