我的应用中的Android通知会随机显示

时间:2019-03-14 14:26:01

标签: java android android-notifications

我的应用程序中的Android通知是随机显示的,我需要它在用户指定的指定时间显示,就像Google Clock应用程序一样。 有人可以帮我吗?

下面是我用来设置通知的代码。

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            Calendar calendar_alarm = Calendar.getInstance();
            Calendar calendar_now = Calendar.getInstance();

            calendar_now.setTimeInMillis(System.currentTimeMillis());

            calendar_alarm.setTimeInMillis(System.currentTimeMillis());

            calendar_alarm.set(Calendar.HOUR_OF_DAY, mHour);
            calendar_alarm.set(Calendar.MINUTE, mMin);
            calendar_alarm.set(Calendar.SECOND, 0);

            if (calendar_alarm.before(calendar_now)) {
                calendar_alarm.add(Calendar.DATE, 1);
            }

            Intent i = new Intent(getApplicationContext(), MyBroadcastReciever.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1200, i, PendingIntent.FLAG_UPDATE_CURRENT);
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar_alarm.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

            dialog.dismiss();
        }
    });

0 个答案:

没有答案