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