1

我試圖發出通知,顯示一年中某一天發生的情況。我似乎無法弄清楚如何獲得Android。我怎樣才能讓android每天在這個應用程序上檢查?活動關閉時不顯示通知

cal = Calendar.getInstance(); 
    dayOfYear = cal.get(Calendar.DAY_OF_YEAR); 

    payDays = new int[12]; 

    payDays[0] = 60; 
    payDays[1] = 88; 
    payDays[2] = 123; 
    payDays[3] = 151;... 

    // Checks when to send the notification 
    if (dayOfYear == payDays[0] || 
     dayOfYear == payDays[1] || 
     dayOfYear == payDays[2] || 
     dayOfYear == payDays[3] || 

    { 

     Builder builder = new Notification.Builder(this) 
     .setContentTitle("Phone Commission") 
     .setContentText("Expand for totals") 
     .setSmallIcon(R.drawable.notify2); 


     Notification notification = new Notification.BigTextStyle(builder) 
       .bigText("Add-a-Line: " + AALNote + 
         "\nUpgrades: " + UPGNote + 
         "\nNew Lines: " + NLNote + 
         "\nYour total commission is: " + "$" + totalNote).build(); 

      NotificationManager notify = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
       notify.notify(0, notification); 

    } 

回答

0

您需要設置報警。你不能在活動中這樣做。您應該在服務中設置該警報,因爲在活動完成後,在活動中這樣做會使其丟失。

+0

這個API是AlarmManager? – user1985295 2013-02-27 05:22:21

+0

是的。它基本上允許你設置一個PendingIntent,當時間流逝時會被調用。你可以讓它使用超精確的版本或不太準確的版本。對於你的情況,我會使用不太準確的,幾秒鐘就不會有問題。請注意,這在重新啓動時不起作用,爲此,您必須讓廣播接收器收聽BOOT_COMPLETE並啓動您的服務。 – 2013-02-27 05:27:38

+0

感謝球員們的意見 – user1985295 2013-02-27 05:28:16

相關問題