2013-06-03 87 views
1

首先对不起我的英语。 我有一个报警列表,我需要在特定的时间用通知唤醒应用程序。我一次只放一个闹钟。 我把设置的功能,它的工作报警,然后我把下一个报警广播,但它不会在时间醒来。为什么? 毫秒是不同且正确的,但报警不起作用。AlarmManager Android设置第二次不工作

public static void setNextAlarma(long milisegundos){ 
    Bundle extras = new Bundle(); 
    extras.putString("mensaje", "message"); 
    Intent i = new Intent(InfoApp.ALERT_MANAGER); 
    i.putExtras(extras); 

    PendingIntent pintent = PendingIntent.getBroadcast(InfoApp.miContexto, (int) milisegundos, i, 0); 

    if (milisegundos != 0){ 
     InfoApp.miContexto.registerReceiver(AlertasBrCast, new IntentFilter(InfoApp.ALERT_MANAGER)); 

     AlarmManager alarm = (AlarmManager)InfoApp.miContexto.getSystemService(Context.ALARM_SERVICE); 

     alarm.set(AlarmManager.RTC_WAKEUP, milisegundos, pintent); 

    } 
    else{ 
     AlarmManager alarm = (AlarmManager)InfoApp.miContexto.getSystemService(Context.ALARM_SERVICE); 
     alarm.cancel(pintent); 
    } 
} 

public final static BroadcastReceiver AlertasBrCast = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      Bundle extras = intent.getExtras(); 

      String mensaje = ""; 
      if (extras != null) 
       mensaje = extras.getString("mensaje"); 

      generateNotification(context, mensaje, Calendario.class, null); 

      updateAlarm(); 
     } 
    }; 

    public void updateAlarm(){ 
// Consult the next alarm in the database 
long fechaNuevaMilli = (Utilidades.strToDate(nuevaFecha, 
        InfoApp.formatoSQL)).getTime(); 


      Utilidades.setNextAlarma(fechaNuevaMilli); 
} 

谢谢

+0

哪里是'updateAlarm的代码(真)'? –

+0

我咨询数据库中的下一个警报,然后拨打setNextAlarm把新的 – user1852854

回答

0

警报。设置(AlarmManager.RTC_WAKEUP,milisegundos,pintent);将设置报警仅一次用户setRepeating函数指定延迟间隔

这里是完美的解决方案

Alarm Manager Example

+0

谢谢。有用! – user1852854

+0

我有这个问题。它可以工作,但如果应用程序在后台,警报不起作用。哪里有问题?谢谢! – user1852854