2012-08-28 67 views
3

我試圖啓動一個不可取消的正在進行的通知,但不管我嘗試它是否可以取消。關於未決意圖通知的問題

代碼:

NotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    int icon = R.drawable.btn_rating_star_on_pressed_holo_light; 
    long when = System.currentTimeMillis(); 

    notification = new Notification(icon, getString(R.string.notificationMSG), when); 
    notification.ledARGB = 999; 

    Intent notificationIntent = new Intent(this, MainActivity.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, Notification.FLAG_ONGOING_EVENT + Notification.FLAG_NO_CLEAR + Notification.FLAG_SHOW_LIGHTS); 

    notification.setLatestEventInfo(context, getString(R.string.app_name), getString(R.string.notificationMSG), contentIntent); 

    notificationManager.notify(Global.NOTIFICATION_ID,notification); 

我也想知道如何使一旦啓動它的活動結束後它去了。試圖做到以下,但它只有一些作品。

@Override 
protected void onDestroy(){ 
    notificationManager.cancel(Global.NOTIFICATION_ID); 
    super.onDestroy(); 
} 
+0

沒有工作。就好像沒有任何標誌實際被激活。 LED也不工作。 –

+0

看我的答案! – Carnal

回答

2
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

notification.flags |= Notification.FLAG_ONGOING_EVENT; 
notification.flags |= Notification.FLAG_NO_CLEAR; 
notification.flags |= Notification.FLAG_SHOW_LIGHTS; 

對於一些愚蠢的原因,我們不能把標誌直接在PendingIntent和需要設置它們來代替這種方式。