我的Android应用程序中有一个通知系统,当通知时间到达通知栏中显示的通知时,当用户点击它时,我的应用程序打开,但通知栏中仍显示通知。如何在用户点击通知栏时隐藏通知?
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Calendar now = GregorianCalendar.getInstance();
Bundle bund = intent.getExtras();
String text = bund.getString("name", "");
Notification.Builder mBuilder =
new Notification.Builder(context)
.setSound(android.provider.Settings.System.DEFAULT_NOTIFICATION_URI)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle( text)
.setContentText("text");
Toast.makeText(context,"ok", Toast.LENGTH_LONG).show();
Intent resultIntent = new Intent(context, Main.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(Main.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
// }
}
}
答案 0 :(得分:2)
你试试吗?
mBuilder.setAutoCancel(true);