2013-12-16 68 views
0

我正在使用AlarmManager來重複報警。我爲週一,週四和週六設置了重複鬧鐘。鬧鈴在週一正確顯示,但週四到來時,它會一起顯示兩個通知,並在週六顯示全部三個通知。我該如何對其進行編碼,以便在第一次報警完成時,它不會在同一周的隨後報警中顯示,而是會在接下來的幾周內顯示?過去的通知顯示當前通知

EDIT1 - 代碼

MainActivity

public boolean onOptionsItemSelected(MenuItem item) { 
     // TODO Auto-generated method stub 

     switch (item.getItemId()) { 
     case R.id.action_settings: 
      EnableNoti(); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 

    } 

    private void EnableNoti() { 
     Calendar calendar = Calendar.getInstance(); 
     Calendar calendar1 = Calendar.getInstance(); 
     Calendar calendar2 = Calendar.getInstance(); 
     Calendar calendar3 = Calendar.getInstance(); 

     calendar.set(Calendar.DAY_OF_WEEK, 1); // Sunday 
     calendar1.set(Calendar.DAY_OF_WEEK, 4); // Wednesday 
     calendar2.set(Calendar.DAY_OF_WEEK, 6); // Friday 
     calendar3.set(Calendar.DAY_OF_WEEK, 5); // Thursday 
     calendar3.set(Calendar.DAY_OF_WEEK_IN_MONTH, 1); // First Thursday of 
                  // Each Month 
     Date now = new Date(System.currentTimeMillis()); 
     if (calendar3.getTime().before(now)) { 
      calendar3.add(Calendar.MONTH, 1); 
     } else if (calendar.getTime().before(now)) { 
      calendar.add(Calendar.HOUR, 168); 
     } else if (calendar1.getTime().before(now)) { 
      calendar1.add(Calendar.HOUR, 168); 
     } else if (calendar2.getTime().before(now)) { 
      calendar2.add(Calendar.HOUR, 168); 
     } 

     // Sunday 
     calendar.set(Calendar.HOUR_OF_DAY, 9); 
     calendar.set(Calendar.MINUTE, 0); 
     calendar.set(Calendar.SECOND, 0); 
     calendar.set(Calendar.AM_PM, Calendar.AM); 

     // Wednesday 
     calendar1.set(Calendar.HOUR_OF_DAY, 17); 
     calendar1.set(Calendar.MINUTE, 30); 
     calendar1.set(Calendar.SECOND, 0); 
     calendar1.set(Calendar.AM_PM, Calendar.PM); 
     // Friday 
     calendar2.set(Calendar.HOUR_OF_DAY, 17); 
     calendar2.set(Calendar.MINUTE, 30); 
     calendar2.set(Calendar.SECOND, 0); 
     calendar2.set(Calendar.AM_PM, Calendar.PM); 

     // Thursday 
     calendar3.set(Calendar.HOUR_OF_DAY, 22); 
     calendar3.set(Calendar.MINUTE, 0); 
     calendar3.set(Calendar.SECOND, 0); 
     calendar3.set(Calendar.AM_PM, Calendar.PM); 

     Intent myIntent = new Intent(TheBeginningEnglish.this, 
       MyReceiverEnglish.class); 
     pendingIntent = PendingIntent.getBroadcast(TheBeginningEnglish.this, 0, 
       myIntent, 0); 

     Intent myIntent1 = new Intent(TheBeginningEnglish.this, 
       MyReceiver1English.class); 
     pendingIntent1 = PendingIntent.getBroadcast(TheBeginningEnglish.this, 
       0, myIntent1, 0); 

     Intent myIntent2 = new Intent(TheBeginningEnglish.this, 
       MyReceiver2English.class); 
     pendingIntent2 = PendingIntent.getBroadcast(TheBeginningEnglish.this, 
       0, myIntent2, 0); 

     Intent myIntent3 = new Intent(TheBeginningEnglish.this, 
       MyReceiver3English.class); 
     pendingIntent3 = PendingIntent.getBroadcast(TheBeginningEnglish.this, 
       0, myIntent3, 0); 

     AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE); 
     alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, 
       calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, 
       pendingIntent); // every Sunday 
     alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, 
       calendar1.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, 
       pendingIntent1); // every Wednesday 
     alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, 
       calendar2.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, 
       pendingIntent2); // every Friday 
     alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, 
       calendar3.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 31, 
       pendingIntent3); // every first Thursday 
    } 

Receiver.java

public class MyReceiverEnglish extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) 
    { 
     Intent service = new Intent(context, MyAlarmServiceEnglish.class); 
     context.startService(service); 

    } 

} 

AlarmService.java

public class MyAlarmServiceEnglish extends Service { 
    int currentapiVersion = android.os.Build.VERSION.SDK_INT; 
    private NotificationManager mManager; 
    Notification notification; 

    // Notification notification; 

    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     // TODO Auto-generated method stub 
     super.onCreate(); 
    } 

    @Override 
    public void onStart(Intent intent, int startId) { 
     onStartCommand(intent, 0, startId); 
    } 

    @SuppressWarnings({ "static-access", "deprecation" }) 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     mManager = (NotificationManager) this.getApplicationContext() 
       .getSystemService(
         this.getApplicationContext().NOTIFICATION_SERVICE); 

     Intent intent1 = new Intent(this.getApplicationContext(), 
       TheBeginning1English.class); 
     if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB) { 
      Notification notification = new Notification(
        R.drawable.ic_launcher, 
        "Its Sunday!.", 
        System.currentTimeMillis()); 
      intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP 
        | Intent.FLAG_ACTIVITY_CLEAR_TOP); 

      PendingIntent pendingNotificationIntent1 = PendingIntent 
        .getActivity(this.getApplicationContext(), 0, intent1, 
          PendingIntent.FLAG_UPDATE_CURRENT); 
      notification.flags |= Notification.FLAG_AUTO_CANCEL; 
      notification.setLatestEventInfo(this.getApplicationContext(), 
        "Testing", "Its Sunday.", 
        pendingNotificationIntent1); 

      mManager.notify(0, notification); 
     } else { 
      PendingIntent pendingNotificationIntent1 = PendingIntent 
        .getActivity(this.getApplicationContext(), 0, intent1, 
          PendingIntent.FLAG_UPDATE_CURRENT); 
      Notification notification = new NotificationCompat.Builder(this) 
        .setContentTitle("Test Test App") 
        .setContentText("Its Sunday!.") 
        .setWhen(System.currentTimeMillis()) 
        .setContentIntent(pendingNotificationIntent1) 
        .setSmallIcon(R.drawable.ic_launcher).build(); 
      notification.flags |= Notification.FLAG_AUTO_CANCEL; 
      mManager.notify(0, notification); 

     } 
     return START_NOT_STICKY; 
    } 

    @Override 
    public void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
    } 

} 

就是這樣,在主要活動中,您將看到四個單獨的日曆變量,我必須創建一個Receiver,Receiver1,Receiver2,Receiver3和AlarmService1,2,3。對於每個日曆,不是最好的方式,但只有我能想到的東西。我添加了:

Date now = new Date(System.currentTimeMillis()); 
if (calendar3.getTime().before(now)) { 
    calendar3.add(Calendar.MONTH, 1); 
} else if (calendar.getTime().before(now)) { 
    calendar.add(Calendar.HOUR, 168); 
} else if (calendar1.getTime().before(now)) { 
    calendar1.add(Calendar.HOUR, 168); 
} else if (calendar2.getTime().before(now)) { 
    calendar2.add(Calendar.HOUR, 168); 
} 

我今天添加了日期部分,沒有檢查出來知道它是否會工作。感謝您幫助我!

回答

0

Alaram Service如何工作?

它根據您的條件開始,就像您爲日,周或任何一週設置的一樣。所以在每個指定的時間設置它接收命令。像通知,語氣。

so onStartServiceCommand()方法。你必須爲此應用邏輯。

因此,該通知僅在該特定日期發生一次。

+0

謝謝,我使用的代碼@ http://stackoverflow.com/questions/20445481/cant-get-alarmmanager-and-multiple-notifications-to-work/20445815?noredirect=1#comment30546480_20445815 ...雖然現在我修改了它爲每個鬧鐘創建一個廣播服務nd服務(我是一個業餘愛好者)...如果yhu可以檢查d代碼,我會qlad –

+0

好的..請顯示您的代碼.. –

+0

謝謝Sanket,請檢查我的編輯! –