在android中取消通知警报

时间:2013-09-24 11:05:37

标签: android android-notifications android-alarms

我实施了一个警告:

        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        calendar.set(Calendar.SECOND, 0);
        alarmManager.setRepeating(AlarmManager.RTC,
                calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
                pendingIntent);

和broadcastReceiver:

public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    Intent service1 = new Intent(context, WeekTickAlarmService.class);
    context.startService(service1);
}

}

和服务:

 public class WeekTickAlarmService extends Service {

@SuppressWarnings({ "static-access", "deprecation" })
@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);

    mManager = (NotificationManager) this.getApplicationContext()
            .getSystemService(
                    this.getApplicationContext().NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this.getApplicationContext(),
            MainActivity.class);
    intent1.putExtra("notification", true);
    Notification notification = new Notification(R.drawable.icon,
            Shares.getStringFromResources(this.getApplicationContext(),
                    R.string.alarm_service_notification_message),
            System.currentTimeMillis());
    notification.sound = RingtoneManager
            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
            this.getApplicationContext(), 0, intent1,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(this.getApplicationContext(), Shares
            .getStringFromResources(getApplicationContext(),
                    R.string.alarm_service_notification_bar_title), Shares
            .getStringFromResources(getApplicationContext(),
                    R.string.alarm_service_notification_bar_description),
            pendingNotificationIntent);
    mManager.notify(666, notification);
}
}

我的问题是,当我点击通知时,通知会消失,但在某些设备中,系统会多次通知!

1 个答案:

答案 0 :(得分:0)

这样的事情:

public void CancelAlarm(Context context) {

        Intent intent = new Intent(context, MyReceiver .class);
        PendingIntent sender = PendingIntent
                .getBroadcast(context, 0, intent, 0);
        AlarmManager alarmManager = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        alarmManager.cancel(sender);
    }