2016-07-22 19 views
2

在我的應用程序中,我添加了一個正在進行的通知。通知在狀態欄上有一個圖標,在通知區域有更多詳細信息。Android - 如何將狀態欄上的進行中的事件,但不通知屏幕上?

此圖片來自Google。這是在通知區域:

This image is from Google. This is the notification area

我需要在狀態欄上的圖標會在那裏住總,但用戶將能夠解僱通知屏幕的通知,以便用戶將與只停留狀態欄上的圖標。 我該怎麼辦?

我的服務代碼:

@SuppressWarnings("static-access") 
    @Override 
    public int onStartCommand(Intent intent, int flag, int startId) 
    { 
     super.onStartCommand(intent, START_STICKY, startId); 

     ... 

     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
      .setContentTitle(getString(R.string.myNotificationString)) 
      .setContentText(string1 + " | " + string2) 
      .setLargeIcon(appIcon) 
      .setSmallIcon(getResources().getIdentifier("icon_" + getCurrentIconNumber(),"drawable", getPackageName())); // the icon changes every day 

     Intent resultIntent = new Intent(this, MainActivity.class); 
     PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0 ,resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
     mBuilder.setContentIntent(resultPendingIntent); 
     Notification notification = mBuilder.build(); 
     notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT; 

     mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     mNotifyMgr.notify(myRequestCode, notification); 

     return START_STICKY; 
    } 
+0

抱歉,但我目前還不清楚我編輯的問題 – ddb

+0

。簡而言之:假設我有一個應用程序向用戶顯示電池狀態。用戶希望在狀態欄上看到電池狀態,但他不想將其始終視爲通知區域中的通知。所以我希望用戶能夠關閉通知區域中的通知,但狀態欄上的圖標不會消失。 – TamarG

+0

像鬧鐘主動通知?您可以刪除已設置鬧鐘的通知,但如果您將其刪除,您總是會在狀態欄中看到應用程序圖標。我明白了嗎? – ddb

回答

0

嘗試定義resultPendingIntent像下面

PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0 ,resultIntent, PendingIntent.FLAG_ONGOING_EVENT); 

編輯1

@SuppressWarnings("static-access") 
    @Override 
    public int onStartCommand(Intent intent, int flag, int startId) 
    { 
     super.onStartCommand(intent, START_STICKY, startId); 

     ... 

     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
      .setContentTitle(getString(R.string.myNotificationString)) 
      .setContentText(string1 + " | " + string2) 
      .setLargeIcon(appIcon) 
      .setSmallIcon(getResources().getIdentifier("icon_" + getCurrentIconNumber(),"drawable", getPackageName())); // the icon changes every day 

     Intent resultIntent = new Intent(this, MainActivity.class); 
     PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0 ,resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
     mBuilder.setContentIntent(resultPendingIntent); 
     Notification notification = mBuilder.build(); 
     notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT; 

     mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     mNotifyMgr.notify(myRequestCode, notification); 

     return START_STICKY; 
    } 
+0

但我有「\t notification.flags | = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;」 - 這不夠嗎?無論如何,它不起作用,因爲用戶不能解僱通知,它是粘性的。 – TamarG

+0

好的,我明白了。然後恢復該更改並進行此更改'notification.flags = Notification.FLAG_ONGOING_EVENT;' – ddb

+0

對不起,試試這個'notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;',所以去掉'|'在分配新建分配FY – ddb

相關問題