使用应用程序,当相机亮起时,它会显示电池状态。它工作正常,但只显示进度条只有几秒钟,然后显示该栏为空。电池状态的文本值始终是正确的。
public class MainActivity extends Activity implements OnClickListener {
public static Camera cam = null
public int ison = 0;
MediaPlayer click;
private ProgressBar pg;
private TextView batInfo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
configureImageButton();
click = MediaPlayer.create(this, R.raw.click);
pg = (ProgressBar)findViewById(R.id.pBar);
pg.setMax(100);
batInfo = (TextView) findViewById(R.id.batInfo);
this.registerReceiver(this.batteryinfoReceiver, new IntentFilter (Intent.ACTION_BATTERY_CHANGED));
}
private BroadcastReceiver batteryinfoReceiver = new BroadcastReceiver () {
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
batInfo.setText("%"+level);
final float[] roundedCorners = new float [] {5,5,5,5,5,5,5,5};
ShapeDrawable pgDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners,null,null));
String MyColor = level>10?(level<=30?"#ffc800":"#00ff00"):"#ff0000";
pgDrawable.getPaint().setColor(Color.parseColor(MyColor));
ClipDrawable progress = new ClipDrawable(pgDrawable,Gravity.LEFT,ClipDrawable.HORIZONTAL);
pg.setProgressDrawable(progress);
pg.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.progress_horizontal));
pg.setProgress(level);
}
};