Reputation: 7014
I have Android application which is Sales Rep application.When he using the app first need to show the notification like your battery level is 10% ..., then after 2 minutes if he doesn't plug the power , it automatically need to shut down the device.
I have created like this
public class BatteryLevelActivity extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra("level", 0);
Toast.makeText(context, "Battery Level is "+level+"%", Toast.LENGTH_LONG) ;
}
}
And My androidManifest File
<receiver android:name=".service.BatteryLevelActivity">
<intent-filter>
<action android:name="android.intent.action.BATTERY_CHANGED" />
</intent-filter>
</receiver>
And Calling place i did like this
batteryLevelReceiver = new BatteryLevelReceiver();
IntentFilter intentFilter1 = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
intentFilter1.addAction(Intent.ACTION_BATTERY_LOW);
registerReceiver(batteryLevelReceiver, intentFilter1);
It go to BatteryLevelReceiver
class. I really don't know How to check power is plugged or not
&& how to power off automatically after showing the notification ?
Please anybody help me on this ...
Thanks in advance..
Upvotes: 1
Views: 1676
Reputation: 13541
Once you have determined how to detect the level of power in the battery then I'd recommend instead of throwing a notification to the bar, just send the shutdown command to the device, this will internally generate the shutdown dialog.
Update
http://developer.android.com/reference/android/content/Intent.html#ACTION_SHUTDOWN
Upvotes: 2
Reputation: 67286
To check that the device is plugged in or not you can try this stuff,
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;
}
For shutting down device here
is a thread that you will like to check and for checking the plugged in status already posted the method from this
thread.
Upvotes: 1
Reputation: 4119
Check for ACTION_BATTERY_CHANGED , BATTERY_PLUGGED_USB or BATTERY_PLUGGED_AC Hope this will help : How to detect power connected state?
Upvotes: 1