2017-07-09 25 views
0

讓我有一套時間。例如,{"11:40","11:55","16:15","20:05"}。 首先,我需要在11:40上顯示一些通知,然後在11:55等等,直到結束。每個通知都有自己的標題和文本。而且他們不應該更新(我的意思是,如果用戶沒有按下或在狀態欄中刪除11:40顯示的通知,則不應將下一個通知置於前一通知上)。我知道如何使用AlarmManagerBroadcastReceiver顯示通知,但我沒有一個正確的想法如何解決這個問題。我想象的解決方案如下:當應用程序第一次打開時,我設置警報顯示通知index = 0(其中index是時間陣列的索引)。當顯示第一個通知時,我更新index併爲下一個通知等設置警報,直到結束。最後,所有操作都會取消,並且不會顯示任何通知。但我無法想象如何構建該想法。如何解決這個問題呢?可能嗎?我需要一個Service這個嗎?你能給出有用的步驟來解決這個問題嗎?如何輪流顯示通知?

回答

0

你只需要在給定的時間調用這個方法。

private void notificationManager(Context context, String message,long time) { 

    try { 
     long when = //Set your own time here by passing it in the function 

     NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 

     Log.v("message", "," + message); 

     Intent notificationIntent = new Intent(context, SplashActivity.class); 


     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0); 

     NotificationCompat.Builder builder = new NotificationCompat.Builder(context); 
     builder.setAutoCancel(true); 
     builder.setContentTitle(context.getString(R.string.app_name)); 
     builder.setStyle(new NotificationCompat.BigTextStyle().bigText(message)); 
     builder.setContentText(message); 
     builder.setTicker(message); 
     builder.setLights(Color.GREEN, 500, 500); 
     builder.setWhen(when); 
     builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); 
     builder.setSmallIcon(R.mipmap.ic_launcher); 
     builder.setContentIntent(intent); 


     Notification notification = builder.build(); 
     notification.ledARGB = 0xFFff0000; 
     notification.flags = Notification.FLAG_SHOW_LIGHTS; 
     notification.ledOnMS = 100; 
     notification.ledOffMS = 100; 
     notificationManager.notify(1, notification); 

     PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); 
     PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG"); 
     wl.acquire(15000); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

} 

只是打發時間的builder.setWhen以毫秒爲單位,你可以通過創建一個日曆對象,並設置時間在裏面做。它會自動安排這些通知並顯示它們各自的時間。使用for循環來傳遞函數。

希望這hepls

+0

setWhen(長時間)不用於設定日期和時間觸發的通知。請參閱文檔:https://developer.android.com/reference/android/app/Notification.Builder.html#setWhen(long) https://stackoverflow.com/questions/15458705/how-can-i -set時間和日期換通知 – abay