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.
Asked
Active
Viewed 312 times
0
-
what does mean of "showing the app on screen" ?? – curiousMind Mar 01 '16 at 05:54
-
5Possible duplicate of [Get battery level and state in Android](http://stackoverflow.com/questions/3291655/get-battery-level-and-state-in-android) – Amsheer Mar 01 '16 at 05:58
1 Answers
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