2013-10-17 36 views
0

我想顯示正在進行的通知。但它仍然顯示在通知部分。 我爲我的通知使用了自定義佈局。通知將不會「正在進行中」

這裏是我的代碼:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    int icon = android.R.drawable.alert_dark_frame; 
    long when = System.currentTimeMillis(); 
    Notification notification = new Notification(icon, "Hi", when); 
    RemoteViews contentView = new RemoteViews(getPackageName(), 
      R.layout.custom_notification); 
    notification.contentView = contentView; 

    Intent notificationIntent = new Intent(this, MainActivity.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
      notificationIntent, 0); 

    notification.contentIntent = contentIntent; 
    notification.defaults |= Notification.FLAG_ONGOING_EVENT; 
    mNotificationManager.notify(0, notification); 
} 

編輯: 你可以看到這個圖片,看看我想做的事情。 http://backcountrynavigator.com/wp-content/uploads/screenshots/download_ongoing_notification.png

+1

標記爲「正在進行的事件」的通知仍然是通知。這就是它出現在通知列表中的原因。這基本上是一個粘性通知,無法使用清除按鈕從列表中清除。 – tiguchi

+0

你是什麼意思「但它仍然顯示在通知部分」。你不希望它有通知顯示在那裏嗎? –

+0

@NobuGames我認爲這是Notification.FLAG_NO_CLEAR,使通知unclerable。 –

回答

0

你應該Service.startForeground()除了NotificationManager.notify()

編輯開始正在進行的通知:我也建議你使用Notification.Builder,而不是新的通知(),然後用setOngoing(),只是爲了保持它的清潔。

+0

謝謝! 我想我需要爲我的應用程序提供服務,然後再考慮通知。 我將支持API級別8,但不支持通知構建器! –

+0

@NimaAhmadi如果您想要支持Level 8 API,則可以使用Android兼容性庫API http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html – Leaudro

相關問題