点击后删除通知

我希望通知将在用户点击之后closures。 我看到每个人都说使用标志,但我无法find任何地方的标志,因为我使用NotificationCompat.Builder类而不是通知类。 有人有任何想法如何使自己的通知删除?
这是我设置通知时的代码:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle("New Question") .setContentText("" + QuestionData.getAuthor().getUserName() + ": " + QuestionData.getQuestion() + ""); Intent openHomePageActivity = new Intent("com.example.ihelp.HOMEPAGEACTIVITY"); TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); stackBuilder.addNextIntent(openHomePageActivity); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent( 0, PendingIntent.FLAG_UPDATE_CURRENT ); mBuilder.setContentIntent(resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(0, mBuilder.build()); 

很简单,只需调用这个:

 mBuilder.setAutoCancel(true); 

另外,虽然没有必要,但如果您真的想使用FLAG_AUTO_CANCEL ,只需在调用mNotificationManager.notify之前调用mNotificationManager.notify

 mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL; 

尝试这个….

 NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); .......... NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( this).setSmallIcon(R.drawable.push_notify_icon) .setContentTitle("New Question!") .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)) .setAutoCancel(true).setContentText("" + QuestionData.getAuthor().getUserName() + ": " + QuestionData.getQuestion() + ""); mBuilder.setContentIntent(contentIntent); .............. mBuilder.getNotification().flags |= Notification.FLAG_AUTO_CANCEL; mNotificationManager.notify(0, mBuilder.build()); 

您可以为通知添加一个标志:

http://developer.android.com/reference/android/app/Notification.html#FLAG_AUTO_CANCEL

这会在点击时消除。