2014-03-06 83 views
2

我試圖創建通知果凍豆(API 16)之後,並有得到的問題與我的通知,這是我的代碼通知Android不關閉點擊

public class CreateNotification extends AsyncTask<Void, Void, Void> { 

     int style = NORMAL; 

     public CreateNotification(int style) { 
      this.style = style; 
     } 
     @Override 
     protected Void doInBackground(Void... params) { 
      Notification noti = new Notification(); 

      noti = setNormalNotification(); 

      noti.defaults |= Notification.DEFAULT_LIGHTS; 
      noti.defaults |= Notification.DEFAULT_VIBRATE; 
      noti.defaults |= Notification.DEFAULT_SOUND; 

      noti.flags |= Notification.FLAG_ONLY_ALERT_ONCE; 

      mNotificationManager.notify(0, noti); 

      return null; 

     } 
    } 


private Notification setNormalNotification() { 
     Bitmap remote_picture = null; 

     remote_picture = getBitmapFromURL(sample_url); 

     // Setup an explicit intent for an ResultActivity to receive. 
     Intent resultIntent = new Intent(this, DetailActivity.class); 

     // TaskStackBuilder ensures that the back button follows the recommended convention for the back key. 
     TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 

     // Adds the back stack for the Intent (but not the Intent itself). 
     stackBuilder.addParentStack(ResultActivity.class); 

     // Adds the Intent that starts the Activity to the top of the stack. 
     stackBuilder.addNextIntent(resultIntent); 
     PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); 

     return new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.ic_launcher) 
       .setAutoCancel(true) 
       .setLargeIcon(remote_picture) 
       .setContentIntent(resultPendingIntent) 
       .addAction(R.drawable.ic_launcher, "Open detail", resultPendingIntent) 
       .addAction(R.drawable.ic_launcher, "Close", resultPendingIntent) 
       .setContentTitle("Normal Notification") 
       .setContentText("This is an example of a Normal Style.").build(); 
    } 

在該行

.addAction(R.drawable.ic_launcher, "Open detail", resultPendingIntent) 
.addAction(R.drawable.ic_launcher, "Close", resultPendingIntent) 

如果我點擊打開詳細信息關閉按鈕,通知不會關閉..如何解決?謝謝,對不起我的英文

+1

你應該找到的東西在這個有用:http://stackoverflow.com/questions/11883534/how-to-dismiss-android-notification-after-action-has-been-clicked – user3151134

+0

我讀這就是,但我不知道如何實現:D – bukanamay

回答

2

當你創建你的resultIntent添加通知ID。你的情況是你在mNotificationManager.notify(0, noti);定義的所以添加這樣的:

resultIntent.putExtra("NOTIFICATION_ID", 0); 

然後你可以檢索ID取消您resultPendingIntent像這樣的活動的OnCreate通知:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    manager.cancel(getIntent().getIntExtra("NOTIFICATION_ID", -1)); 
    //todo 
}