0

Can I get a code for android app which run in background and continuously checks the battery percentage and notify me when battery level reaches to 7 percentage, I tried its installing but not showing the app on screen.

Bartłomiej Semańczyk
  • 52,820
  • 43
  • 206
  • 318

1 Answers1

2

Here we listen to a Intent (ACTION_BATTERY_CHANGED) and register a receiver when the intent is fired. In ActionScript, this is i believe is DispatchEvent, and we would call mBatInfoReceiver and get the current level of our battery life and put that int value to our textfield or TextView.

public class Main extends Activity {
  private TextView contentTxt;
  private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context arg0, Intent intent) {
      int level = intent.getIntExtra("batterylevel", 7);
      contentText.setText(String.valueOf(level) + "%");
    }
  };

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main_layout);
    contentText = (TextView) this.findViewById(R.id.mspaceTxt);
    this.registerReceiver(this.mBatInfoReceiver, 
    new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
  }
}
Smeeran
  • 129
  • 5