取消多个警报?

时间:2016-03-09 07:13:05

标签: java android alarmmanager android-notifications android-alarms

我创建了一个活动。我已经在10分钟,30分钟之前和每小时之前提供通知选项,直到12小时之前。我必须为所有这些间隔设置通知。

现在我想在开关关闭时取消特定通知。我已经给出了取消通知的相同意图。但我认为所有通知都被取消了。

我也创建了时间表,并使用时间表的id创建事件。现在,如果我删除一个时间表,我想删除设置为属于已删除时间表的事件的所有通知。

我该怎么做?

设置通知时间功能:

public void setNotificationTime(Calendar c)
{

    Date dateFrom = new Date();
    df = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy");
    try {
        dateFrom = df.parse(startTime);
    }
    catch (ParseException ex) {

    }

    dateFrom.getTime();
    c.setTime(dateFrom);

    hour = c.get(Calendar.HOUR_OF_DAY);
    minute = c.get(Calendar.MINUTE);

    if(notificationTime !=null  &&  !notificationTime.isEmpty()) {


        if (notificationTime.equals("10 Minutes Before")) {


            c.set(Calendar.HOUR_OF_DAY, hour);
            c.set(Calendar.MINUTE, minute - 10);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            c.set(Calendar.DATE, day);
            // c.set(Calendar.DAY_OF_WEEK,);

            SetDay(c);

            notification = c.getTime();
            notificationTime = df.format(notification);

            setAlarm(c);

            Toast.makeText(getApplicationContext(), notificationTime, Toast.LENGTH_SHORT).show();


        } else if (notificationTime.equals("30 Minutes Before")) {

            c.set(Calendar.HOUR_OF_DAY, hour);
            c.set(Calendar.MINUTE, minute - 30);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            c.set(Calendar.DATE, day);
            // c.set(Calendar.DAY_OF_WEEK,);

            SetDay(c);

            notification = c.getTime();
            notificationTime = df.format(notification);

            Toast.makeText(getApplicationContext(), notificationTime, Toast.LENGTH_SHORT).show();

            setAlarm(c);
        } else if (notificationTime.equals("1 Hour Before")) {

            c.set(Calendar.HOUR_OF_DAY, hour - 1);
            c.set(Calendar.MINUTE, minute);
            c.set(Calendar.SECOND, 0);
            c.set(Calendar.MILLISECOND, 0);
            c.set(Calendar.DATE, day);
            // c.set(Calendar.DAY_OF_WEEK,);

            SetDay(c);

            notification = c.getTime();
            notificationTime = df.format(notification);

            Toast.makeText(getApplicationContext(), notificationTime, Toast.LENGTH_SHORT).show();


            setAlarm(c);
        }

设置闹钟功能:

    @SuppressLint("NewApi")
private void setAlarm(Calendar targetmCalen) {


    intent = new Intent(getBaseContext(), NotificationReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
    alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent);

}

关闭时通知取消:

 alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                intent = new Intent(getApplicationContext(), NotificationReceiver.class);
                pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), RQS_1, intent, 0);
                alarmManager.cancel(pendingIntent);

通知接收方:

public class NotificationReceiver  extends BroadcastReceiver {


public static int MY_NOTIFICATION_ID = 0;
NotificationManager notificationManager;
Notification myNotification;

EventTableHelper db;

@Override
public void onReceive(Context context, Intent intent) {


    Toast.makeText(context, "Time is set", Toast.LENGTH_LONG).show();

    db = new EventTableHelper(context);

    List<EventData> testSavings = db.getAllEvents();

    for (EventData ts : testSavings) {
        String log = "from date:" + ts.getFromDate()
                + " ,to date: " + ts.getToDate()
                + " ,location: " + ts.getLocation()
                + " ,title " + ts.getTitle();

        Calendar c = Calendar.getInstance();
        Date date = new Date();
        Date date1 = new Date();
        Log.d("Result: ", log);

        SimpleDateFormat df = new SimpleDateFormat("E MMM dd hh:mm:ss zzzz yyyy");
        SimpleDateFormat df2 = new SimpleDateFormat("hh:mm a");

        try {
            date = df.parse(ts.getFromDate());
            date1 = df.parse(ts.getToDate());
        } catch (ParseException ex) {

        }
        String timeFrom = df2.format(date);
     //   String startTime = String.valueOf(timeFrom);

        String timeTo = df2.format(date1);
       // String endTime = String.valueOf(timeTo);


        String location = ts.getLocation();
        String title = ts.getTitle();


        Intent myIntent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(
                context,
                0,
                myIntent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        if(location.equals(""))
        {
            String msg = "From : " + timeFrom + "\nTo : " + timeTo;

            myNotification = new NotificationCompat.Builder(context)
                    .setContentTitle("Event : " + title)
                    .setContentText(msg)
                    .setWhen(System.currentTimeMillis())
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                    .setSmallIcon(R.drawable.eventicon)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                    .setDefaults(Notification.DEFAULT_SOUND)
                    .build();

        }

        else
        {
            String msg = "From : " + timeFrom + "\nTo : " + timeTo + "\nAt : " + location;
            myNotification = new NotificationCompat.Builder(context)
                    .setContentTitle("Event : " + title)
                    .setContentText(msg)
                    .setWhen(System.currentTimeMillis())
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(true)
                    .setSmallIcon(R.drawable.eventicon)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
                    .setDefaults(Notification.DEFAULT_SOUND)
                    .build();

        }

        Log.i("Notify", "Notification");
        notificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(MY_NOTIFICATION_ID, myNotification);

        myNotification.flags=Notification.FLAG_AUTO_CANCEL;

        Intent i = new Intent();
        i.putExtra("notificationId",MY_NOTIFICATION_ID);

        MY_NOTIFICATION_ID ++;

    }
    }
 }

我应该做哪些改变才能达到预期效果?

1 个答案:

答案 0 :(得分:0)

您需要创建不同的待处理意图,然后使用特定待处理意图取消特定警报。像往常一样创建待处理意图,但为不同警报添加不同标记。

   pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1,   intent, FLAG);//Set different value of Flag at the time creating alarm

所以为了使它通用,只需在你的

中添加一个参数
   setAlarm(Calendar targetmCalen,int flag)