如何设置使用setExact以及如何取消同样的重复闹钟? [英] How to set repeating alarm using setExact and how to cancel the same?

查看:429
本文介绍了如何设置使用setExact以及如何取消同样的重复闹钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置使用报警管理器和广播接收器的通知。我曾尝试使用闹钟经理setInexactRepeating方法,但它剂量不会提高上述API 19日在准确的时间报警。

I am setting notifications using alarm manager and broadcast receiver. I have tried to use setInexactRepeating method of an alarm manager but it dose not raise alarms at exact time above API 19.

所以我有一个建议,使用setExact方法和手动设置报警。我不知道我能做到这一点。我需要计算每星期日期一年?

So I got a suggestion to use setExact method and set alarms manually. I don't know how can I do this. Do I need to calculate dates for every week for a year?

另外,我创建了报警我想,如果该事件被删除,删除相同。我怎样才能取消呢?使用cancel方法我都试过取消报警。对于测试我试图设置多个闹钟,并删除它。

Also the alarms I am creating I want to delete the same if the event is deleted. How can I cancel this? Using cancel method I have tried to cancel the alarm. For test I tried to set multiple alarms and delete it.

如果我删除一个事件,使事件的推崇报警应该得到解除,但可能是所有的警报越来越取消,因为我不删除的事件后得到任何通知。

If I delete one event so the respected alarm of that event should get cancel but may be all alarms are getting cancel as I do not get any notification after deleting an event.

我使用的唯一ID这一点。作为最后的静态INT RQS_1 = 1;设置报警。

I have used unique Id for this. As final static int RQS_1 = 1; for setting alarm.

任何人都可以帮我这个好吗?

Can Anyone help me with this please?

设置闹钟:

 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.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);

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

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

    }



    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();

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

    }
}

setDay方法我创建设置已通过choosen从对话框的用户,并设置一天的日子。

setDay method I have created to set the day which has been choosen by user from a dialog and set day.

setDay功能:

public void SetDay(Calendar c)
{
    switch (dayOfWeek)
        {
            case  "Mon":
                c.set(Calendar.DAY_OF_WEEK, 2);
                c.getTime();
                Toast.makeText(getApplicationContext(),dayOfWeek,Toast.LENGTH_SHORT).show();
                break;
            case "Tue":
                c.set(Calendar.DAY_OF_WEEK, 3);
                c.getTime();
                Toast.makeText(getApplicationContext(),dayOfWeek,Toast.LENGTH_SHORT).show();
                break;

}

通知接收器

 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;

        }
    }
}

是忘了加code取消:

Yes forgot to add code of cancel:

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

感谢您..

推荐答案

与此类似,

pendingIntent = PendingIntent.getBroadcast(getBaseContext(),RQS_1, intent, 0);
    alarmManager.cancel(pendingIntent); //Remove any alarms with a matching Intent

有关更多信息 AlarmManager

这篇关于如何设置使用setExact以及如何取消同样的重复闹钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆