2013-05-15 60 views
2

我有一個可能顯示通知的服務,問題出現在通知設置完成後,點擊時或者刷卡時都不會清除。我使用的標誌Notification.FLAG_AUTO_CANCEL,但它似乎並沒有做任何事情..通知用戶清除通知或點擊一次

private NotificationManager nm; 
nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
private final int NOTIFY_ID = 1; 
private void showNotification(String date, String name) { 
     try{ 
      CharSequence text = "You have new Event"; 
      Notification notification = new Notification(R.drawable.small_icon, text, System.currentTimeMillis()); 
      PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, viewEvents.class).putExtra("date", date), 0); 
      notification.setLatestEventInfo(this, name, "Date: "+date, contentIntent); 
      notification.defaults |= Notification.DEFAULT_SOUND; 
      notification.defaults |= Notification.DEFAULT_VIBRATE; 
      notification.defaults |= Notification.DEFAULT_LIGHTS; 
      notification.defaults |= Notification.FLAG_AUTO_CANCEL; 
      nm.notify(NOTIFY_ID, notification); 
     }catch(Exception e){ 
      Log.i("Notification",e.toString()); 
     } 
    } 

那我做錯了嗎?

回答

3

嘗試使用Notification.BuilderNotificationCompat.Builder,而不是手動滾動Notification

此外,這將防止您的代碼中的錯誤,您應用FLAG_AUTO_CANCELdefaults而不是flags

0

Notification.FLAG_AUTO_CANCEL已在您的代碼中註釋掉。

但在情況下,它是沒有,那麼標誌Notification.DEFAULT_LIGHTS應notification.defaults被設置好的不notification.flags

+0

我也評論說:「Notification.FLAG_AUTO_CANCEL」後,我發現,這是行不通的,我試圖將它設置爲「標誌」和「默認」,但兩者都不起作用。 – Khaled

0

我使用此代碼顯示/隱藏通知:

private Handler handler = new Handler(); 
private Runnable task = new Runnable() { 
@Override 
public void run() { 
    NotificationManager manager = (NotificationManager) contesto.getSystemService(Context.NOTIFICATION_SERVICE); 
    Intent launchIntent = new Intent(contesto.getApplicationContext(), SearchHistory.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(contesto.getApplicationContext(), 0, launchIntent, 0); 
    //Create notification with the time it was fired 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(contesto); 
    builder.setSmallIcon(R.drawable.ic_launcher).setTicker("MESSAGE HERE!").setWhen(System.currentTimeMillis()).setAutoCancel(true).setDefaults(Notification.DEFAULT_SOUND).setContentTitle("TITLE").setContentText("MESSAGE").setContentIntent(contentIntent); 
    Notification note = builder.build(); 
    manager.notify(100, note); 
} 
}; 

以及調用只是把:

handler.post(task);