【问题标题】:How to Set AlarmManager For notification that has to be repeated multiple times a day and everyday如何为每天和每天重复多次的通知设置 AlarmManager
【发布时间】:2015-04-06 17:57:57
【问题描述】:

我可以为每 24 小时触发一次的警报设置 AlarmManager:

    Calendar c= Calendar.getInstance();
    c.setTimeInMillis(System.currentTimeMillis());
    c.set(Calendar.HOUR_OF_DAY,holder.hours);
    c.set(Calendar.MINUTE,holder.min);
    Intent in=new Intent(Reminder_entry.this,Notificationservice.class);
    in.putExtra("name",holder.name);
    PendingIntent pi=PendingIntent.getService(Reminder_entry.this, holder.pi,      in,PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManageram=AlarmManager)Reminder_entry.this.getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), 1000 * 60 * 60 *24,pi);

但我无法设置它,因此它可以每天每 6 小时重复一次。 我的研究表明我将不得不菊花链警报所以如果一个响起我必须设置为第二天。你能帮我理解如何做到这一点吗?如何在触发警报并处理我的待处理意图时重置警报,因为我的待处理意图正在调用 A 服务,而我不知道如何在服务中设置警报。

这是我的服务:

public class Notificationservice extends Service {
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
  String name=intent.getStringExtra("name");
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Intent i=new Intent(Notificationservice.this,Notification_landing.class);
    PendingIntent pi=PendingIntent.getActivity(Notificationservice.this, 0, i,PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder b= new NotificationCompat.Builder(Notificationservice.this);
    b.setAutoCancel(true).setContentTitle(name).setContentText("time to take "+name).setSmallIcon(R.drawable.ic_launcher).setSound(soundUri);
    b.setContentIntent(pi);
    Notification n=b.build();
    NotificationManager nm=(NotificationManager)Notificationservice.this.getSystemService(NOTIFICATION_SERVICE);

    nm.notify(1,n);
    return super.onStartCommand(intent, flags, startId);
}}

【问题讨论】:

    标签: android notifications android-service alarmmanager reminders


    【解决方案1】:

    我最近也实施了一项带有警报的服务,但我不会自称是专家,因此更有经验的用户可能不同意我的方法。

    在我的服务中,我在 onHandleIntent() 方法中完成大部分真正的“工作”,该方法被 IntentService 覆盖

    在你的情况下,我认为这是一个工作,然后在你希望它再次运行时设置另一个 6 小时的闹钟。例如:

         @Override
         protected void onHandleIntent(Intent intent) {
            // do the work that needs to be done every 6 hours
            someWork();
    
           // Now create a new PendingIntent and associate it with an alarm for 6 hours time
           // This example uses the current Intent and replays it but you could
           // modify it if you need to perform something different next time.
    
           PendingIntent pIntent = PendingIntent.getService(this, 1234, intent, 
                    PendingIntent.FLAG_CANCEL_CURRENT);
    
           AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    
           // Find out system time in milliseconds for 6 hours in the future
           long scheduledTime = System.currentTimeMillis() + (6 * 60 * 60 * 1000);
    
           // Set the alarm. Note that this version is using RTC - there are other options,
           // read the docs for more info: 
           // developer.android.com/reference/android/app/AlarmManager.html
    
           am.set(AlarmManager.RTC, scheduledTime, pIntent);
        }
    

    请注意,我选择不使用Calendar 来计算警报的未来时间 - 我认为仅使用以毫秒为单位的系统时间就不那么尴尬了。

    更新 - 一些额外的 cmets

    1. 从您的代码中,我看到您正在从 Service 扩展 - 我认为从 IntentService 扩展更容易,因为 Android 已经为您实现了更多的关键功能,因此覆盖 onHandleIntent(Intent) 更多或少你需要做的。
    2. 我上面的答案使用AlarmManager 而不是NotificationManager,但我认为菊花链的原理保持不变。

    【讨论】:

    • 那么当你以前的警报触发你的设置这个新警报时?我的意思是我的应用程序是一个药物提醒,所以用户可能必须每隔 5 小时吃一次药,比如每隔一天。想象一下今天是星期一,所以在重复 4 次后,我将不得不在 48 小时后重新设置闹钟,然后每 5 小时触发一次。
    • 是的,在前一个警报触发后设置新警报。因此,在您的情况下,您可能需要添加一些额外的逻辑来确定它是另外 2 天还是 5 小时的警报。例如也许有一个警报,然后每 5 小时再发出 4 个警报,然后在 48 小时后发出一个警报(然后重复循环)。
    • 终于破解了,非常感谢您的帮助。如果我存储未决意图的唯一 if ,如果用户选择删除药物,它可以用来取消警报吗?
    • 我以前没有这样做过,但这听起来应该是可能的,但我还没有检查过。不过,请进一步查看PendingIntent 的 Android SDK 文档 - 我确信其中有一些东西。或者,如果您找不到您正在寻找的答案,请提出与此相关的新 SO 问题。很高兴我能帮上忙!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 2020-01-11
    相关资源
    最近更新 更多