设置重复警报如何影响onCreate()

时间:2016-06-16 09:03:57

标签: android alarmmanager android-notifications android-pendingintent

如果我在onCreate中调用setinexactrepeating()警报方法,它如何影响以指定间隔重复的警报

以下是设置闹钟的代码,我在onCreate()

中调用此方法
public void setAlarm()
{
    Intent myintent=new Intent(this,AlarmReciever.class);

    Random random = new Random();


    int ran = random.nextInt(total_words.size());


    String tempString=onlySearch(total_words.get(ran), 1);
    myintent.putExtra("word", total_words.get(ran));
    myintent.putExtra("meaning", tempString);
    myintent.putExtra("language", 1);


    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);


    PendingIntent pendingIntent=PendingIntent.getBroadcast(this,101,myintent,PendingIntent.FLAG_UPDATE_CURRENT);

    //NotificationTime is the sharedPreference file, in which i am storing hours and minute got from timepicker        

    alarmManager.cancel(pendingIntent);
    Calendar calendar=Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, NotificationTime.getInt("hour", 12));
    calendar.set(Calendar.MINUTE, NotificationTime.getInt("min", 0));
    calendar.set(Calendar.SECOND, 0);


    if(calendar.before(Calendar.getInstance()))
        calendar.add(Calendar.DATE,1);



    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY
            , pendingIntent);


    System.out.println("ALARM SET STATUS");
}

警报首先在确切时间完美安排,但不会重复。

实际上我想在指定时间每天显示通知,并且我正在传递带有意图的字符串。默认情况下我设置为中午12:00,但用户可以更改时间。设置时间(或第一个警报)后的第一个通知正在工作,但重复无效。

请看一下,如果有人知道更好的解决方案,每天在同一指定时间显示通知,那么如果你告诉我,我们将不胜感激。

提前致谢

1 个答案:

答案 0 :(得分:0)

用户setRepeating用于重复警报

 AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
/* Repeating on every interval */
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);