0

我把闹钟定在午夜。但是设置警报会触发我的广播接收器?我在应用程序启动时设置了闹钟。编码:

    //set the alarm
    protected void onCreate(Bundle savedInstanceState) {
            as=new AlarmSetter();
            as.SetAlarma(this); .........

    //the AlarmSetter
    public class AlarmSetter  {

        public void SetAlarma(Context context){

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.AM_PM, Calendar.AM); 

         Intent intent = new Intent(context, AlarmKicked.class);
         PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1333333, intent, PendingIntent.FLAG_UPDATE_CURRENT);
         AlarmManager am =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
         am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,
                 pendingIntent);
        }

    }


    //and my Broadcast reciever 
    public class AlarmKicked extends BroadcastReceiver{



        @Override
        public void onReceive(Context context, Intent intent) {

            NotificationsA.Notizer(context);

            System.out.println("IT IS MIDNIGHT! SETTING TIMER..");

        }

    }

//Manifest
<receiver android:name=".AlarmKicked" android:process=":remote"/>

当我启动应用程序时,我设置了 Allarm.. 但同时我进入了 Logcat:它是午夜!设置计时器.. 是什么在开始时触发了我的广播接收器?

编辑:

作品!:

public void SetAlarma(Context context){

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.HOUR, 0);
    calendar.add(Calendar.DAY_OF_MONTH, 1);
    calendar.set(Calendar.AM_PM, Calendar.AM); 
4

1 回答 1

1

您设置为午夜,但是您的日历时间现在是当天的午夜,这已经过去了。

这会导致警报在注册后立即响起。

引用文档

如果时间发生在过去,将立即触发警报,警报计数取决于触发时间相对于重复间隔过去多长时间。

相反,在您的日历中添加一天,这应该可以阻止它发生。

Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.AM_PM, Calendar.AM); 
        calendar.add(Calendar.DATE, 1); 
于 2013-02-16T17:49:37.867 回答