setRepeating()不能正确重复

时间:2013-11-22 23:03:07

标签: java android notifications alarmmanager

我正在使用AlarmManager每天在特定时间内通过时间选择器选择发出通知。通知在同一天正确启动,但每天都没有正确重复!!!

这是使用setRepeating()设置通知的方法:

public void witer_reminder(View view)
{
    am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    Intent intent = new Intent(this, Witer_Notification.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
                                    intent, PendingIntent.FLAG_CANCEL_CURRENT);

    Calendar cal = Calendar.getInstance();
    Calendar calSet = (Calendar) cal.clone();

    calSet.set(Calendar.HOUR_OF_DAY, picker.getCurrentHour());
    calSet.set(Calendar.MINUTE, picker.getCurrentMinute());
    calSet.set(Calendar.SECOND, 0);
    calSet.set(Calendar.MILLISECOND, 0);

    if(calSet.compareTo(cal) <= 0)
    {
        // Today Set time passed, count to tomorrow
        calSet.add(Calendar.DATE, 1);
    }

    am.setRepeating(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(),
                                            24 * 60 * 60 * 1000, pendingIntent);
}

这是BroadcastReciver类:

public class Witer_Notification extends BroadcastReceiver
{
    NotificationManager nm;

    @Override
    public void onReceive(Context context, Intent intent)
    {
        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                                    new Intent(context, MainActivity.class), 0);

        PendingIntent actiontIntent = PendingIntent.getActivity(context, 0,
                                    new Intent(context, Suggestion.class), 0);

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("")
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(""));

        mBuilder.setContentIntent(contentIntent);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        // mBuilder.setStyle(new NotificationCompat.InboxStyle());

        NotificationManager mNotificationManager = (NotificationManager)
                        context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());
    }
}
BTW,应用程序目标是SDK 19。

1 个答案:

答案 0 :(得分:2)

  

我发现setExact()替换为set()

正确。

  

它不适用于intervalAtMilis作为参数

不直接。但是,当您通过BroadcastReceiver事件控制setExact()时,请再次致电setExact()以安排下一个事件。

  

我没有为setRepeating()

找到任何东西

没有简单的解决方案,因为Google试图向您指出这对电池不利。如上所述使用setExact()是您完全重复的唯一选择。