user2409402
user2409402

Reputation: 181

How to get battery level using broadcast reciever

In my App I want to check battery level if it is less than a specific value I want my App to say that level using text to speech and services. I have done it using broadcastreciever but it works only when App is running on screen and do not work when it is running in background...

public class Batteryreciever extends BroadcastReceiver {
    int level;
    @Override
    public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.intent.action.BATTERY_CHANGED")) { 
            level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
            if (level < 10) {
                String batterylevel = "critical battery ";
        Intent i = new Intent(context, MyService.class);
            i.putExtra("KEY1", batterylevel);
                i.putExtra("type", "battery");
            context.startService(i);
            }       
            }
    } }

Upvotes: 0

Views: 1009

Answers (5)

Thanh Le
Thanh Le

Reputation: 773

take a look at this, it will get you the battery level you need so you can start your service. Just use BatteryManager.EXTRA_LEVEL as the key to get the level from the intent

https://developer.android.com/training/monitoring-device-state/battery-monitoring.html

Upvotes: 0

Anjali Tripathi
Anjali Tripathi

Reputation: 1477

try this link to get battery information.

public class Main extends Activity {
  private TextView batteryTxt;
  private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context ctxt, Intent intent) {
      int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
      batteryTxt.setText(String.valueOf(level) + "%");
    }
  };

  @Override
  public void onCreate(Bundle b) {
    super.onCreate(b);
    setContentView(R.layout.main);
    contentTxt = (TextView) this.findViewById(R.id.batteryTxt);
    this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
  }
}

Upvotes: 0

user2409402
user2409402

Reputation: 181

Battery broadcast receiver declared in manifest file does not work? The answer to my question is here, actually in manifest you can not register broadcast receiver for battery change ..

Upvotes: 0

user2894435
user2894435

Reputation:

using sticky Broadcast receiver you can get Battery level and after that as you told battery level goes down to certain level then perform task what ever you want.

Link and See This

it will help and you get more information

Upvotes: 1

Harshit Rathi
Harshit Rathi

Reputation: 1862

use this method :

public static float getBatteryLevel(Context cntx) {
        Intent batteryIntent = cntx.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

        // Error checking that probably isn't needed but I added just in case.
        if(level == -1 || scale == -1) {
            return 50.0f;
        }

        return ((float)level / (float)scale) * 100.0f; 
    }

Upvotes: 0

Related Questions