如何启动通知自定义日期和放大器;时间? [英] How to start notification on custom date&time?

查看:104
本文介绍了如何启动通知自定义日期和放大器;时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何某些click事件之后开始通知:X毫秒。一个code这样

I know how to start a notification X milliseconds after some click event. A code like this

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

凡code的通知看起来像这样

Where the code for notification looks like this

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点?

How can I set notification to appear on specific date at specific time, let say October 1, 7 PM?

推荐答案

我认为最好的办法就是创建设置通知的服务,然后激活使用的 AlarmManager
这是我的$ C $下这样做。 这是在code为AlarmManager:

I think that the best way will be to create a service that sets the notification and then activate the service using an AlarmManager.
Here is my code for doing that. That's the code for the 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);
    }

}

这篇关于如何启动通知自定义日期和放大器;时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆