0

我能设置AlarmManager报警被触发每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小时重复讲话。 我的研究表明,我将不得不雏菊链警报器,所以如果一个熄灭,我必须为第二天设置。你能帮助我理解如何做到这一点吗?我怎样才能重置警报,当它被触发,我的待决意图处理,因为我的未决意图调用服务,我不知道如何设置服务中的警报。

这是我的服务:

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); 
}} 

回答

2

我最近实现的服务与警报,太,但不会声称自己是一个专家,所以有经验的用户可以与我的做法不以为然。

在我的服务,我做的大部分是从IntentService

覆盖在你的情况下,在onHandleIntent()方法真正的“工作”,我认为这是做的工作,然后设置其他报警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为制定未来的闹钟时间 - 我认为这是不太尴尬只是使用以毫秒为单位的系统时间。

更新 - 一些补充意见

  1. 从你的代码,我看你是从Service延伸 - 我认为这是更容易从IntentService延伸Android已经实现了很多的关键功能的你这样重写onHandleIntent(Intent)或多或少就是你需要做的。
  2. 我上面的回答使用AlarmManager而不是NotificationManager,但我认为菊花链的原理仍然是一样的。
+0

那么当你以前的报警触发你的设置这个新的报警?我的意思是我的应用程序是一种药物提醒,因此用户可能每隔5天每隔5小时就要吃药。想象今天是星期一,所以在4次重复之后,我必须在48小时后每5小时重置闹钟。 – 2015-04-06 08:53:58

+0

是的,在前一次发射后设置新的警报。 因此,对于您的情况,您可能需要添加一些额外的逻辑来确定是否为另外2天或5小时的警报。例如也许有一个警报,接着是每5小时4次,然后是48小时后再次发出警报(然后重复周期)。 – aoemerson 2015-04-06 09:35:28

+0

终于破解了,非常感谢你的帮助。如果我存储唯一的待处理意图,如果用户选择删除药物,是否可用于取消警报? – 2015-04-06 17:23:05