Java 电池电量不足的通知

Java 电池电量不足的通知,java,android,eclipse,android-activity,Java,Android,Eclipse,Android Activity,当设备的电池电量不足15%时,我试图弹出一个通知。 但它每次都会显示通知,当我有超过15%的信息时也是如此 这是我的代码: public class MainActivity extends Activity { public int battery, level; private static final int DIALOG_EXIT=1; Button Hscore,ToInfo,ToProj; ImageView Easter,EXIT,Help,Info; private Text

当设备的电池电量不足15%时,我试图弹出一个通知。 但它每次都会显示通知,当我有超过15%的信息时也是如此

这是我的代码:

public class MainActivity extends Activity {

public int battery, level;
private static final int DIALOG_EXIT=1;
Button Hscore,ToInfo,ToProj;
ImageView Easter,EXIT,Help,Info;

private TextView contentTxt;    
public BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
        @Override
        public void onReceive(Context arg0, Intent intent) {
          // TODO Auto-generated method stub
          level = intent.getIntExtra("level", 0);   

          contentTxt.setText(String.valueOf(level) + "%  Battery");      

        }
      };



@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

    contentTxt = (TextView) this.findViewById(R.id.txtbattery);

    if(level <= 15)
    {
        Intent Charge = new Intent(MainActivity.this,ChargeActivity.class);
        //  Intent st =new Intent(android.provider.Settings.ACTION_SETTINGS);       
            String title="you have low battery", message="charge your phone";
            int icon = R.raw.duck_yellow;    
            int mNotificationId = 001;

            PendingIntent resultPendingIntent =
                    PendingIntent.getActivity(
                            getBaseContext(),
                            0,
                            Charge,
                            PendingIntent.FLAG_CANCEL_CURRENT
                    );   
            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                    getBaseContext());
            Notification notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
                    .setAutoCancel(true)
                    .setContentTitle(title)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                    .setContentIntent(resultPendingIntent)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setLargeIcon(BitmapFactory.decodeResource(getBaseContext().getResources(), R.raw.duck_yellow))
                    .setContentText(message).build();

            NotificationManager notificationManager = (NotificationManager) getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(mNotificationId, notification);
    }
公共类MainActivity扩展活动{
公共int电池,电平;
私有静态最终int对话框_EXIT=1;
按钮Hscore、ToInfo、ToProj;
图像查看、退出、帮助、信息;
私有文本视图contentTxt;
public BroadcastReceiver mBatInfoReceiver=新的BroadcastReceiver(){
@凌驾
公共void onReceive(上下文arg0,意图){
//TODO自动生成的方法存根
级别=intent.getIntExtra(“级别”,0);
contentTxt.setText(String.valueOf(level)+%Battery);
}
};
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.registerReceiver(this.mBatInfoReceiver,新的IntentFilter(Intent.ACTION\u BATTERY\u CHANGED));
contentTxt=(TextView)this.findViewById(R.id.txtbattery);

if(level)您正在检查onCreate()中的level是否<15。但此时level仍未初始化。您可以记录其值。是否为0?。您必须将if置于onReceive()中。您正在检查onCreate()中的level是否<15。但此时level仍未初始化。您可以记录其值。是否为0?。您必须将if置于onReceive()中。您正在检查onCreate()中的级别是否<15。但此时该级别仍未初始化。您可以记录其值。是否为0?。您必须将其置于onReceive()中。