我有一个问题,我无法弄清楚以下代码有四个整数,其中一个是“级别”,在我在代码中的某处更改该值并稍后尝试检索它之后,我得到了默认值,所以有什么这段代码有问题还是我错过了什么
public class Battery_Info extends Activity {
int scale = -1;
int level = -1;
int voltage = -1;
int temp = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_battery__info);
this.setTitle("Battery Information");
IntentFilter filter=new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
final NotificationManager ntifymgr=(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
BroadcastReceiver receive=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
scale=intent.getIntExtra(BatteryManager.EXTRA_SCALE,-1);
level=intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
voltage=intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
temp=intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
TextView t=(TextView)findViewById(R.id.textView5);
t.setText(""+scale);
t=(TextView)findViewById(R.id.textView6);
t.setText(""+level, BufferType.EDITABLE);
t=(TextView)findViewById(R.id.textView7);
t.setText(""+voltage);
t=(TextView)findViewById(R.id.textView8);
t.setText(""+temp);
}
};
registerReceiver(receive, filter);
final int Notif_ID=56734;
Notification note= new Notification(R.drawable.ic_launcher, "Battery Notification", System.currentTimeMillis());
note.flags=Notification.FLAG_ONGOING_EVENT;
PendingIntent intent=PendingIntent.getActivity(this, 0, new Intent(this,Battery_Info.class),PendingIntent.FLAG_CANCEL_CURRENT);
note.setLatestEventInfo(this, "BatPer", "Battery Level: "+level+"%", intent);
ntifymgr.notify(Notif_ID, note);
}
当我更改 level 的值时,它会更改并且 textview 会显示,但是当我在通知中使用它时,它会给我默认值