Main.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical">
<TextView android:id="@+id/mainTextView"android:textSize="40dp"android:layout_width="wrap_content"android:layout_height="wrap_content"/>
<ProgressBar android:max="100"android:id="@+id/mainProgressBar"style="?android:attr/progressBarStyleHorizontal"android:layout_height="wrap_content"android:layout_width="wrap_content"android:minHeight="50dp"android:minWidth="150dp"/>
</LinearLayout>
MainActivity.java
package com.kcgroup.bipb;
import android.app.*;
import android.os.*;
import android.content.*;
import android.widget.*;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerReceiver(bp, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
private BroadcastReceiver bp=new BroadcastReceiver(){
@Override
public void onReceive(Context p1, Intent p2)
{
int level=p2.getIntExtra("level",0);
TextView tv=(TextView)findViewById(R.id.mainTextView);
ProgressBar pb=(ProgressBar)findViewById(R.id.mainProgressBar);
tv.setText("Battery Level:"+Integer.toString(level)+"%");
pb.setProgress(level);
// TODO: Implement this method
}
};
}