从被调用的活动中清除通知

时间:2014-02-03 20:13:38

标签: java android android-notifications

我使用此代码显示通知:

public static void ShowCustomNotification(Context context,String notificationTitle, String notificationMessage)
{

     Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        // intent triggered, you can add other intent for other actions
        Intent intent = new Intent(context, ResultActivity.class);
        intent.putExtra("title", notificationTitle);
        intent.putExtra("body", notificationMessage);
        intent.setFlags(PendingIntent.FLAG_UPDATE_CURRENT
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);

        Notification mNotification = new NotificationCompat.Builder(context)

            .setContentTitle(notificationTitle)
            .setSmallIcon(R.drawable.office_girl)
            .setContentIntent(pIntent)
            .setSound(soundUri)
            .build();


        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, mNotification); 
}

当用户点击通知时,调用活动ResultActivity.class并且一切正常。

我的问题是,当用户点击ResultActivity内部的“关闭”按钮时,如何清除状态栏上的通知?如果用户点击“返回”,我不想清除通知。

感谢。

1 个答案:

答案 0 :(得分:0)

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(YOUR_NOTIFICATION_ID);