public class Main extends Activity {
private TextView contentTxt;
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent intent) {
// TODO Auto-generated method stub
int level = intent.getIntExtra("level", 0);
contentTxt.setText(String.valueOf(level) + "%");
}
};
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
contentTxt = (TextView) this.findViewById(R.id.monospaceTxt);
this.registerReceiver(this.mBatInfoReceiver,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
}Quick Explanation
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.
Source Code
Main Layout
Main.java
Here is the source where i got the this stuff working
[gallery]
5 comments:
Hi, do you know how change screen brightness on G1 programatically? Btw, better will get battery level as here - http://www.maximyudin.com/2009/01/05/android/reading-of-battery-level-on-g1/
try this article
http://almondmendoza.com/2009/01/05/changing-the-screen-brightness-programatically/
Thanks for the article, my question is, to use a broadcast reciever, you will need to specify it in the manifest.xml, how do you do that?
Thank you almond, your Battery tutorial help me lot in my programing.
Now I am try to make a Application which show the remaining time of battery discharge.
Can you halp me out any way.
Thank you.
- Amit Parekh.
@lfmdk this must be so late, but you could either post your receiver in xml or in java code
Post a Comment