【发布时间】:2015-07-24 16:15:04
【问题描述】:
现在在 android 中,我将此代码放在一个活动中,以便在按下按钮时显示通知。
static int notificationCount = 0;
然后
btnNotification.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent notificationIntent = new Intent(AlertsActivity.this,NotificationActivitty.class);
PendingIntent pIntent = PendingIntent.getActivity(AlertsActivity.this,notificationCount,notificationIntent,Intent.FLAG_ACTIVITY_NEW_TASK);
// Construct the notification
Notification.Builder nBuilder = new Notification.Builder(AlertsActivity.this);
nBuilder.setContentTitle("You Have a notification!");
nBuilder.setContentText("See Your Notification");
nBuilder.setSmallIcon(android.R.drawable.btn_star);
nBuilder.setContentIntent(pIntent);
nBuilder.addAction(android.R.drawable.stat_notify_call_mute, "go to", pIntent); // from icecream sandwatch - required api 16
// Build the notification
Notification noti = nBuilder.build(); // required api 16
//Send it to manager
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.notify(notificationCount++,noti);
}
}
);
来自通知管理器,我点击它的任何通知,都会将我重定向到另一个活动(NotificationActivity)
现在我把这段代码用来清除通知,但它只清除 id 为 0 的通知,所以我怎样才能清除当前按下的通知
public class NotificationActivitty extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
manager.cancel(0);
// manager.cancelAll(); // Cancel all notifications for this app. from manager
}
如果可能的话,我需要通过它的 id 清除通知。
【问题讨论】:
标签: android android-intent notifications android-notifications notificationmanager