【问题标题】:How to start notification on custom date&time?如何在自定义日期时间开始通知?
【发布时间】:2011-09-24 17:43:44
【问题描述】:

我知道如何在某个点击事件后 X 毫秒开始通知。像这样的代码

        Timer timer = new Timer();
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                triggerNotification();
            }
        };
        timer.schedule(timerTask, 3000);

通知代码如下所示

CharSequence title = "Hello";
CharSequence message = "Hello, Android!";

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon, "A New Message!", System.currentTimeMillis());

Intent notificationIntent = new Intent(this, AndroidAlarmService.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.setLatestEventInfo(AndroidAlarmService.this, title, message, pendingIntent);
notification.defaults = Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(NOTIFICATION_ID, notification);

如何将通知设置为在特定日期的特定时间出现,比如 10 月 1 日晚上 7 点?

【问题讨论】:

    标签: android notifications


    【解决方案1】:

    我认为最好的方法是创建一个设置通知的服务,然后使用 AlarmManager 激活该服务。
    这是我的代码。 这是 AlarmManager 的代码:

    private void startAlarm() {
        AlarmManager alarmManager = (AlarmManager) this.getSystemService(this.ALARM_SERVICE);
        Calendar calendar =  Calendar.getInstance();
        calendar.set(int year, int month, int date, int hour, int minute, int second);
        long when = calendar.getTimeInMillis();         // notification time
                Intent intent = new Intent(this, ReminderService.class);
                PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0);
                alarmManager.set(AlarmManager.RTC, when, pendingIntent);
            }
    

    这里是服务:

    public class ReminderService extends IntentService {
        private static final int NOTIF_ID = 1;
    
        public ReminderService(){
            super("ReminderService");
        }
    
        @Override
          protected void onHandleIntent(Intent intent) {
            NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            long when = System.currentTimeMillis();         // notification time
            Notification notification = new Notification(R.drawable.icon, "reminder", when);
            notification.defaults |= Notification.DEFAULT_SOUND;
            notification.flags |= notification.FLAG_AUTO_CANCEL;
            Intent notificationIntent = new Intent(this, YourActivity.class);
            PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent , 0);
            notification.setLatestEventInfo(getApplicationContext(), "It's about time", "You should open the app now", contentIntent);
            nm.notify(NOTIF_ID, notification);
        }
    
    }
    

    【讨论】:

    • 如果bergnam 在 10 月 1 日晚上 7 点说,when 变量的结构是什么? long when = 01/10/2011 7PM; ?
    • @bergnam 只需要使用一个 Date 对象设置日期,然后调用 .getTime()
    • SimpleDateFormat dateLongue = new SimpleDateFormat("DD MM yyyy HH:mm:ss"); 以及如何以及为什么调用 .getTime ?
    • @androniennn 我添加了设置日期所需的代码,我现在无法检查,但我认为它会起作用。
    • bergnam,我的回答让你满意吗?如果确实如此,请将其标记为正确。 @androniennn:Calendar calendar = Calendar.getInstance(); calendar.set(int year, int month, int date, int hour, int minute, int second); long when = calendar.getTimeInMillis();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 2018-08-15
    • 2021-09-06
    相关资源
    最近更新 更多