最简单的方法是设置 builder.setAutoCancel(true)
,这样可以在点击通知后自动取消该通知。我希望这段代码能够帮到你。
通知构建器
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(android.R.drawable.btn_star);
builder.setContentTitle("This is title of notification");
builder.setContentText("This is a notification Text");
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
点击通知打开一个活动
Intent intent = new Intent(Broadcastdemo.this, ThreadDemo.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 113,intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
显示构建器通知
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE)
manager.notify(114, builder.build())
完整代码以显示具有图标、图像、标题、描述、自动取消并在单击时打开活动的通知:
public void ShowIntentNotification(View v)
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(android.R.drawable.btn_star);
builder.setContentTitle("This is title of notification");
builder.setContentText("This is a notification Text");
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
Intent intent = new Intent(Broadcastdemo.this, ThreadDemo.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 113,intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(114, builder.build());
}
.setContentIntent(PendingIntent.getActivity(context, id, __new Intent()__, PendingIntent.FLAG_CANCEL_CURRENT));
。但是,点击通知也会隐藏通知面板(如果您有很多需要以这种方式清除的通知,则必须将其拉回)。 - Alex Semeniuk