【问题标题】:AlarmManager Android set doesn't work second timeAlarmManager Android 设置第二次不起作用
【发布时间】:2013-06-10 23:46:51
【问题描述】:

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

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);
}

谢谢

【问题讨论】:

  • updateAlarm(true)的代码在哪里?
  • 我在数据库中查询下一个警报,然后调用 setNextAlarm 放入新的

标签: android alarmmanager


【解决方案1】:

alarm.set(AlarmManager.RTC_WAKEUP, miisegundos, pintent);只有用户 setRepeating 函数指定延迟间隔,才会设置警报

这里是完美的解决方案

Alarm Manager Example

【讨论】:

  • 我有这个问题。它可以工作,但如果应用程序在后台,则警报不起作用。问题出在哪里?谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-13
  • 1970-01-01
  • 2014-01-31
  • 2015-06-21
  • 2011-10-04
  • 1970-01-01
  • 2021-11-13
相关资源
最近更新 更多