0

我有问题,当用户按下通知活动时,它会打开但无法关闭。

这是我的代码:

NotificationManager mNotificationManager = 
     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    int icon = R.drawable.ic_launcher;
    CharSequence tickerText = "Good Morning";
    long time = System.currentTimeMillis();
    Notification notification = new Notification(icon, tickerText, time);
    Context context = getApplicationContext();
    CharSequence contentTitle = "Title";
    CharSequence contentText = "Text";
    Intent notificationIntent = new Intent(this, NotificationManagerDemoActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,0);
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
    mNotificationManager.notify(1,notification);
4

3 回答 3

2

像这样尝试..希望它会起作用.. notification.flags |= Notification.FLAG_AUTO_CANCEL;

于 2012-04-13T05:46:36.177 回答
0

在构建时将 Auto Cancel 设置为 true,Notification如下所示。

  notificationBuilder.setAutoCancel(true);
于 2014-05-13T20:59:09.207 回答
0

您应该尝试编写如下代码

NotificationCompat.Builder mBuilder = 
     new NotificationCompat.Builder(getApplicationContext(), "Channel")
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),
                     R.mipmap.ic_launcher_logo_round))
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.mipmap.ic_launcher_logo_round);
mNotificationManager.notify(id,mBuilder.build());
.setAutoCancel(true) 
于 2020-12-31T14:06:28.193 回答