1

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..

Piraba
  • 6,974
  • 17
  • 85
  • 135

3 Answers3

2

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

JoxTraex
  • 13,423
  • 6
  • 32
  • 45
1

Check for ACTION_BATTERY_CHANGED , BATTERY_PLUGGED_USB or BATTERY_PLUGGED_AC Hope this will help : How to detect power connected state?

Community
  • 1
  • 1
Vins
  • 4,089
  • 2
  • 35
  • 50
1

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.

Community
  • 1
  • 1
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • No.Still not showing Shutdown screen/dialog. I did ` PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "My Tag"); wl.acquire();` – Piraba Jan 17 '12 at 07:48
  • You can checkout [this one](http://stackoverflow.com/questions/6756768/turn-off-screen-on-android) – Lalit Poptani Jan 17 '12 at 07:56