通知来得太频繁,而不是一天一次



我试图建立一个应用程序,每天发送通知同一时间(在中午)。不过,差不多每两个小时就来一次。我做错了什么吗?这是我设置闹钟时间的代码片段。

    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);

请任何帮助是感激的。代码可以工作,但它出现得太频繁

看看这个,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);

相关内容

  • 没有找到相关文章

最新更新