AlarmManager setRepeating不使用每日间隔

时间:2014-06-22 13:31:58

标签: android notifications alarmmanager

我在尝试设置每日通知时遇到了问题。

我使用AlarmManager每天下午6点设置闹钟

Intent myIntent = new Intent(this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent,
    PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
if (calendar.get(Calendar.HOUR_OF_DAY) >= 18) {
    calendar.add(Calendar.DATE, 1);
}

calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.HOUR_OF_DAY, 18);
alarmManager.cancel(pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
    calendar.getTimeInMillis(), 24*60*60*1000,
    pendingIntent);

但是我不知道为什么第一次警报会在18:00准时发出,但下一次警报会随机发出。有时是19:30,有时是20:06。我只是不明白我错在哪里。

我尝试了INTERVAL_DAY24*60*60*1000 - 没有任何效果。 但它间隔1分钟,5分钟,10分钟,1小时等工作正常。

AlarmReceiver:

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent service1 = new Intent(context, MyAlarmService.class);
        context.startService(service1);
        System.out.println("alarm");
    }
}

MyAlarmService:

public class MyAlarmService extends Service {
     private NotificationManager mManager;

     @Override
     public IBinder onBind(Intent arg0) {
       // TODO Auto-generated method stub
        return null;
     }

    @Override
    public void onCreate() {
       // TODO Auto-generated method stub  
       super.onCreate();
    }

    @SuppressWarnings("static-access")
    @Override
    public void onStart(Intent intent, int startId) {
       super.onStart(intent, startId);

       mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
       Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);

       Notification notification = new Notification(R.drawable.ic_launcher,"Test notification", System.currentTimeMillis());

       intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);

       PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
       notification.flags |= Notification.FLAG_AUTO_CANCEL;
       notification.setLatestEventInfo(this.getApplicationContext(), "Test", "Test a notification", pendingNotificationIntent);

       mManager.notify(0, notification);
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
    }
}

1 个答案:

答案 0 :(得分:2)

你有两个问题:

  1. setRepeating()在Android 4.4+上不准确,android:targetSdkVersion为19或更高。

  2. 您没有使用WakefulBroadcastReceiver,我的WakefulIntentService或您自己的WakeLock,因此设备可以在闹钟触发任何时间之间重新入睡你的服务完成工作的时间。

  3. 在这种情况下,只需将代码从onStart()(BTW,已被弃用大约5年)移动到onReceive()并完全摆脱服务,就可以更轻松地解决#2问题。 。如果您正在进行磁盘I / O或网络I / O,那么您只需要一个服务;提高Notification应该很便宜。