0

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);
            }       
            }
    } }
user2409402
  • 181
  • 1
  • 8
  • Post your code please. If you are creating the broadcast receiver in the activity it might not work when app is in background, however if you have separate class for the receiver, and then register it in manifest, receiver will be called each time the action is broadcast. See this [link](http://developer.android.com/reference/android/content/BroadcastReceiver.html#onReceive(android.content.Context, android.content.Intent)) – Lexandar Feb 28 '14 at 10:45
  • I have added my code if you may please help me. @AleksandarApostolov – user2409402 Mar 03 '14 at 09:50

5 Answers5

1

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

0

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

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Thanh Le
  • 763
  • 4
  • 13
0

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; 
    }
Harshit Rathi
  • 1,862
  • 2
  • 18
  • 25
0

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));
  }
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Anjali Tripathi
  • 1,477
  • 9
  • 28
0

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

Community
  • 1
  • 1
user2409402
  • 181
  • 1
  • 8