2012-08-09 67 views
1

我已經測試了新的BigTextStyle通知顯示正確。但是,當我向通知添加操作時,它將永遠不會取消,並且單擊該操作時,下拉通知區域永遠不會關閉。我做錯了什麼?通知不會自動取消時addAction

Drawable d = getResources().getDrawable(android.R.drawable.ic_menu_add); 
    Bitmap b = ((BitmapDrawable)d).getBitmap(); 
    Notification noti = null; 

    Intent notificationIntent = new Intent(MainActivity.this, MainActivity.class); 

    String s = "Name: John \n" 
      + "Surname: Doe\n" 
      + "Vehicle: Honda Jazz TA-1234\n" 
      + "Note: Good drifter"; 

    Builder builder = new Notification.Builder(MainActivity.this) 
      .setContentTitle("New challenger arrived!") 
      .setContentText(s) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setLargeIcon(b) 
      .setTicker("New challenger arrived!") 
      .setPriority(Notification.PRIORITY_HIGH) 
      .setAutoCancel(true) 
      .setDefaults(Notification.DEFAULT_ALL) 
      .setNumber(5) 
      .addAction(
        android.R.drawable.ic_menu_add, 
        "Accept", 
        PendingIntent.getActivity(getApplicationContext(), 0, 
          notificationIntent, 0, null)) 
      .addAction(
        android.R.drawable.ic_menu_delete, 
        "Refuse", 
        PendingIntent.getActivity(getApplicationContext(), 0, 
          notificationIntent, 0, null)) 
      .addAction(
        android.R.drawable.ic_dialog_map, 
        "Map", 
        PendingIntent.getActivity(getApplicationContext(), 0, 
          notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT, null)); 

    noti = new Notification.BigTextStyle(builder) 
         .setBigContentTitle("New challenger arrived!") 
         .bigText(s) 
         .setSummaryText("You will never win against me").build(); 

    NotificationManager nm = (NotificationManager) MainActivity.this 
      .getSystemService(Context.NOTIFICATION_SERVICE); 

    nm.notify(0, noti); 

回答

0

我認爲如果您採取措施,您必須手動取消通知。這可以通過呼叫

notifMgr.cancel(0); 

單擊通知本身應該駁回它,但由於某些原因,行動似乎並沒有。你作爲參數傳遞0的原因是becasue這是你給了,當你調用ID:

notifMgr.notify(0, noti); 

這是我更新的答案,不過,我以前回答了這個:

它看起來像你想以啓動您目前顯示的相同活動(來自您的PendingIntent)(我假設這是一個活動應用程序,MainActivity包含上面的代碼)。創建另一個活動,構建PendingIntent以啓動該活動,然後它應該按照您的意願進行操作。或者,運行您的活動,出現通知,導航到主屏幕或不同的應用程序,然後根據您的活動進行操作,您的期望行爲應該顯現。