简体   繁体   English

有每小时/每周/等等…的通知提醒(使用AlarmManager?)

[英]Having a hourly/weekly/etc… notification reminder (with AlarmManager?)

I've looked almost everywhere! 我几乎到处都看过! I can't find any help or tutorials or examples of making a notification trigger weekly, or hourly, or monthly. 我找不到任何帮助,教程或每周,每小时或每月触发通知的示例。 I heard some things about using AlarmManager, but I can't get it working. 我听到了一些有关使用AlarmManager的信息,但无法正常工作。

I haven't worked with Java much (more of an objective-c guy) but I'm having some trouble with this notification system. 我没有使用Java太多(更多的是Objective-C的人),但是我在使用此通知系统时遇到了一些麻烦。 Basically I just want to have a button that when I toggle on will notify the user every week to re-open the app (for example). 基本上,我只想有一个按钮,当我打开该按钮时,它将每周通知用户重新打开该应用程序(例如)。 And to trigger off of course when they don't want to be notified every week to 'open the app.' 当然,当他们不想每周收到通知以“打开应用程序”时触发触发。 Anyway, any ideas? 无论如何,有什么想法吗? I've figured out how to get a notification in general to come up, but I can't get it to delay, or to happen when the app isn't open. 我已经弄清楚了一般情况下如何获得通知,但是我无法延迟或在应用未打开时发生通知。

Thanks! 谢谢!

 Calendar calendar = Calendar.getInstance();

    // 8 AM Each day 
    calendar.set(Calendar.HOUR_OF_DAY, 8);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    PendingIntent pi = PendingIntent.getService(context, 0, new Intent(context, MyClass.class), PendingIntent.FLAG_UPDATE_CURRENT);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);

This will fire alarm at 8AM each day. 每天上午8点将发出警报。 Similarly you can set alarm for any day you like. 同样,您可以在任何时候设置闹钟。

TO GET NOTIFICATION : 获得通知:

  NotificationManager nm;
  nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);        
  CharSequence from = "VIPUL";
  CharSequence message = "Crazy About Android...";
  PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
    new Intent(), 0);
  Notification notif = new Notification(R.drawable.icon,
    "Crazy About Android...", System.currentTimeMillis());
  notif.setLatestEventInfo(context, from, message, contentIntent);
  nm.notify(1, notif);

You have to write this on the activity which you calling from alarm manager. 您必须将其写在您从警报管理器调用的活动上。 This will show you notification. 这将向您显示通知。

You can also set a pending intent which will be called when user click on notification. 您还可以设置待处理的意图,当用户单击通知时将调用该意图。

Indeed you need to take a look at the AlarmManager class and especially on its setRepeating() method. 实际上,您需要查看AlarmManager类,尤其是其setRepeating()方法。 It allows you to set an interval for triggering the action. 它允许您设置触发动作的间隔。 Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM