通知过于频繁,而不是每天一次

时间:2015-09-28 22:58:36

标签: android alarmmanager android-notifications android-alarms repeatingalarm

我正在尝试构建一个每天(中午)同时发送通知的应用。然而,它每两个小时就会发生一次。有什么我做错了吗? 这是我的代码片段,用于设置闹钟时间。

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    dateFormatter = new SimpleDateFormat("dd-MM-yyyy", Locale.US);

    findViewsById();

    setDateTimeField();

    today.set(Calendar.HOUR_OF_DAY, 12);
    today.set(Calendar.MINUTE, 00);
    today.set(Calendar.SECOND, 0);

    Intent myIntent=new Intent(Main.this, MyReceiver.class);
    PendingIntent pendingIntent=PendingIntent.getBroadcast(Main.this,0,myIntent,0);

    AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,today.getTimeInMillis(),24*60*60*1000,pendingIntent);

请任何帮助表示赞赏。代码可以工作,但它太频繁了

1 个答案:

答案 0 :(得分:2)

看看这个, https://developer.android.com/training/scheduling/alarms.html

// Set the alarm to start at approximately 2:00 p.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);

// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, alarmIntent);