Android:AlarmManager发出正确的警报

时间:2012-09-27 07:55:27

标签: android android-alarms

已经回答,对于那些有相同问题的人,请参考此帖的底部


我在我的应用程序上有这个部分,它安排了一个警报,一切运行良好,它正确地安排了警报,但有一个不稳定的行为,有时,当警报被安排被触发,而不是一次点火,它被触发几次我知道这个因为每当一个警报触发我在logcat上显示“ALARM FIRED”时,我的logcat上有几个ALARM FIRED,有时多达10个,我很困惑的是我只打了那个警报一次,并且它多次被触发。这是代码:

这是用于安排我的警报这是以静态方式访问的帮助程序

public final static String ACTION = "com.medical.organizer.utilities.NotifyReciever";
private static NotifyReciever RECIEVER = new NotifyReciever();

public static void scheduleAlarm(Context context,String message,int rCode, long triggerTime)
{
    RECIEVER = new NotifyReciever();
    NotifyAlarm.TRIGGER_TIME = triggerTime;
    NotifyAlarm.CONTEXT = context;

    IntentFilter inFil = new IntentFilter(ACTION);
    NotifyAlarm.CONTEXT.registerReceiver(RECIEVER, inFil);
    Intent intent = new Intent(ACTION);

    intent.putExtra("message", message);
    NotifyAlarm.INTENT = intent;
    NotifyAlarm.startAlarm(rCode);
}
public static void cancelAlarm(Context context, int requestCode)
{
        RECIEVER = new NotifyReciever();
        NotifyAlarm.CONTEXT = context;

        IntentFilter inFil = new IntentFilter(ACTION);
        NotifyAlarm.CONTEXT.registerReceiver(RECIEVER, inFil);
        Intent intent = new Intent(ACTION);
        NotifyAlarm.INTENT = intent;
        //context.unregisterReceiver(RECIEVER);
        NotifyAlarm.stopAlarm(requestCode);
}

这是报警本身

public class NotifyAlarm {
    public static Context CONTEXT;
    public static Intent INTENT;
    public static long TRIGGER_TIME;


    public static void startAlarm(int requestCode)
    {
        AlarmManager am = (AlarmManager) CONTEXT.getSystemService(Context.ALARM_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(CONTEXT, requestCode, INTENT, 0);
        am.set(AlarmManager.RTC_WAKEUP, TRIGGER_TIME, pendingIntent);
        Log.d("AlarmCheck", "Alarm Started for the First Time, set() was called");
    }
    public static void stopAlarm(int requestCode)
    {
        AlarmManager am = (AlarmManager) CONTEXT.getSystemService(Context.ALARM_SERVICE);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(CONTEXT, requestCode, INTENT, 0);
        am.cancel(pendingIntent);
        Log.d("ALARMSTOP","========ALARM WAS STOPPED AT THIS POINT==========");
    }
}

这是我的警报回收(广播接收)

public class NotifyReciever extends BroadcastReceiver {
private Context context;
    @Override
    public void onReceive(Context context, Intent intent) {
    this.context = context;
    Log.d("AlarmCheck","ALARM FIRED!");
        AlertDialog.Builder build = new AlertDialog.Builder(context);
        String message = intent.getStringExtra("message");
        build.setMessage(message);
            build.setCancelable(false);
        build.setPositiveButton("Snooze", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
            //Rechedules the alarm after 25 secs time of interval is for
                    //testing purposes only 
                    NotifyReciever.this.context.unregisterReceiver(NotifyReciever.this);
                Helper.scheduleAlarm(NotifyReciever.this.context, s, 
                  s.getRequestCode(),System.currentTimeMillis()+Helper.TWENTY_FIVE_SEC);

            }
        });
        build.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                //CANCELS THE ALARM     
                    Helper.cancelAlarm(NotifyReciever.this.context, s.getRequestCode());
            }
        });

        AlertDialog alert = build.create();
        alert.show();

    }
}

请注意,接收器不是通过XML声明的,它们是以编程方式注册的,因此支持对话框。

考虑到这种情况,这是我注意到行为总是发生的地方,说,我已经添加了一个时间表并安排了警报@ 5:00 am(当前时间是4:59),所以一开始它完全被解雇了,然后我决定将该警报重新安排到5:01(当前时间5:00),然后发生这种情况,警报会被触发几次。 alear对话框出现几次,必须快速点击全部取消对话框,有时我的应用程序崩溃,因为出现了太多的对话框。

任何人都知道如何解决这个问题,请分享,任何人都知道如何正确安排和发出警报,请分享,以及安排和发射多个警报以及取消它们将非常感激。解释和一些例子会更好。

修改


enter image description here

传说:侧面有一个彩色圆点意味着它在特定时间被点燃,这就是在logcat上登录的情况,比如说4点钟发出警报显示有几个蓝点的警告

编辑这是答案


所以这就是我发现的,我的是你必须只注册你的接收器一次,所以为了避免从使用你的ACTION匹配所有意图的intentFilter多次返回,在我的情况下我已经注册了我的接收器一遍又一遍所以已经有很多意图与相同的ACTIOn因此返回多个接收器并且在指定的时间发出多个警报而不是只发射一个接收器,所以是的,只要你接收器就可以在全球一级注册决定不以XML格式宣布您的接收器

0 个答案:

没有答案