单击后通知android不会关闭
我正在尝试创建通知jelly bean(api 16),但我的通知有问题,这是我的代码单击后通知android不会关闭,android,notifications,Android,Notifications,我正在尝试创建通知jelly bean(api 16),但我的通知有问题,这是我的代码 public class CreateNotification extends AsyncTask<Void, Void, Void> { int style = NORMAL; public CreateNotification(int style) { this.style = style; } @Ov
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();
}
如果单击“打开详细信息”按钮或“关闭”,通知不会关闭。。如何修复它?谢谢,很抱歉在创建结果时使用我的英语添加通知id。在您的情况下,它是您在mNotificationManager.notify(0,noti)中定义的0代码>这样添加它:
resultIntent.putExtra("NOTIFICATION_ID", 0);
然后,您可以检索该id以取消在创建ResultPendingContent的活动时发出的通知,如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(getIntent().getIntExtra("NOTIFICATION_ID", -1));
//todo
}
你应该从中发现一些有用的东西:我已经读过了,但我不知道如何实现:D
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(getIntent().getIntExtra("NOTIFICATION_ID", -1));
//todo
}