取消警报通知

时间:2014-07-02 11:23:18

标签: android notifications android-notifications android-pendingintent android-alarms

我正在尝试取消警报通知,但是虽然我取消了警报通知,但在给定时间,通知不会播放其禁用的警告。

有人可以帮我取消特定通知吗?

我设置了这样的重复通知:

public static void setRepeatingAlarm(String time, int alarmID, String title) {
        Intent intent = new Intent(a, TimeAlarm.class);
        intent.putExtra(Constants.CALC_MSG, title);
        Log.v("--", title + " | " + time);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(a, alarmID,
                intent, PendingIntent.FLAG_CANCEL_CURRENT);
        Calendar cal = Calendar.getInstance();
        String timeArr[] = time.split(":");
        cal.set(Calendar.HOUR_OF_DAY, Integer.valueOf(timeArr[0]));
        cal.set(Calendar.MINUTE, Integer.valueOf(timeArr[1]));
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);

        if (cal.getTimeInMillis() < System.currentTimeMillis()) {
            cal.add(Calendar.DAY_OF_YEAR, 1);
        }
        am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
                (24 * 60 * 60 * 1000), pendingIntent);
    }

我正试图取消它:

public static void cancelNotification(String time, int alarmID, String title) {
        Intent i = new Intent(a, TimeAlarm.class);
        i.putExtra(Constants.CALC_MSG, title);
        PendingIntent p = PendingIntent.getBroadcast(a, alarmID, i, 0);
        am.cancel(p);
        p.cancel();
    }

这是我的警报服务类:

public class TimeAlarm extends BroadcastReceiver {

    NotificationManager nm;

    @SuppressWarnings("deprecation")
    @Override
    public void onReceive(Context context, Intent intent) {
        nm = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        nm.cancelAll();
        CharSequence from = context.getString(R.string.app_name);
        CharSequence message = intent.getStringExtra(Constants.CALC_MSG)
                .toUpperCase();
        int iconRes = R.drawable.ic_stat_notf_icon;
        int notificationID = intent.getIntExtra(Constants.NOTIFICATION_ID, 0);
        Intent notificationIntent = new Intent(context, Main.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                notificationIntent, 0);

        Notification notif = new Notification(iconRes, message,
                System.currentTimeMillis());
        notif.flags = Notification.FLAG_AUTO_CANCEL;
        SharedPreferences prefs = context.getSharedPreferences(
                context.getPackageName(), context.MODE_PRIVATE);
        notif.audioStreamType = AudioManager.STREAM_NOTIFICATION;
        notif.setLatestEventInfo(context, from, message, contentIntent);
        notif.sound = Uri.parse("android.resource://"
                + context.getPackageName() + "/"
                + prefs.getInt(Constants.NOTIF_SOUND, R.raw.al_affassi_full));

        nm.notify(notificationID, notif);
        // r.play();

    }
}

2 个答案:

答案 0 :(得分:1)

试试这个:

public static void stopAlarm(Context context) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent your_intent = new Intent(...);
        PendingIntent your_pending_intent = PendingIntent.getBroadcast(context, 0, your_intent, 0);
        alarmManager.cancel(your_pending_intent);
    }

希望它可以帮助你:)

答案 1 :(得分:1)

待定意图应该是这样的

public static void cancelNotification(String time, int alarmID, String title) {
    Intent i = new Intent(a, TimeAlarm.class);
    i.putExtra(Constants.CALC_MSG, title);
    PendingIntent p = PendingIntent.getBroadcast(a, alarmID, i, PendingIntent.FLAG_UPDATE_CURRENT);
    am.cancel(p);
    p.cancel();
}