Android的设置多个重复闹钟 [英] Android Set multiple repeat alarms

查看:817
本文介绍了Android的设置多个重复闹钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要设置3种不同的报警重复的每一天。(如9:00 am,1:00pm,6:00pm)
我已创建的按钮他们。
我可以设置报警的时候,我按一下按钮。
问题是我如何改变我的code实现,上面提到的。

 私人无效setNotification(){
    意图myIntent =新意图(getActivity(),MyReceiver.class);
    的PendingIntent的PendingIntent = PendingIntent.getBroadcast(getActivity(),0,myIntent,0);
    AlarmManager alarmManager =(AlarmManager)getActivity()getSystemService(Context.ALARM_SERVICE)。
    台历挂历= Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY,set_hour);
    calendar.set(Calendar.MINUTE,set_minute);
    calendar.set(Calendar.SECOND,00);
    长startUpTime = calendar.getTimeInMillis();
    的System.out.println(startUpTime +时间+ System.currentTimeMillis的());
        如果(System.currentTimeMillis的()> startUpTime){
            startUpTime = startUpTime + AlarmManager.INTERVAL_DAY;
        }
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,startUpTime,AlarmManager.INTERVAL_DAY,的PendingIntent);
}


解决方案

据我了解你的问题,你想在一个点击按钮设置报警。
使用以下两种方法来设置报警并取消报警。要重复闹铃使用唯一的请求code在getBroadcast和取消使用相同的请求code值来取消它最初使用报警时启动它。

当按钮被点击呼叫setAlarm方法。

 公共无效setAlarm(日历时间,诠释PK){
        AlarmManager经理=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        意图alarmIntent =新意图(背景下,AlarmReceiver.class);
        的PendingIntent的PendingIntent = PendingIntent.getBroadcast(上下文,PK,alarmIntent,0);
        manager.set(AlarmManager.RTC_WAKEUP,time.getTimeInMillis(),的PendingIntent);
    }
公共无效cancelAlarm(INT PK){
        AlarmManager经理=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        意图alarmIntent =新意图(背景下,AlarmReceiver.class);
        的PendingIntent的PendingIntent = PendingIntent.getBroadcast(上下文,PK,alarmIntent,0);
        manager.cancel(的PendingIntent);
    }

I want to set 3 different alarms repeat everyday.(like 9:00am,1:00pm,6:00pm) And I have created buttons for them. I can set the alarm's time when I click the button. The problem is how can I change my code to achieve above mentioned.

private void setNotification() {
    Intent myIntent = new Intent(getActivity(), MyReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent, 0);
    AlarmManager alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, set_hour);
    calendar.set(Calendar.MINUTE, set_minute);
    calendar.set(Calendar.SECOND,00);
    long startUpTime = calendar.getTimeInMillis();
    System.out.println(startUpTime + "time" + System.currentTimeMillis());
        if (System.currentTimeMillis() > startUpTime) {
            startUpTime = startUpTime + AlarmManager.INTERVAL_DAY;
        }
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, startUpTime, AlarmManager.INTERVAL_DAY , pendingIntent);
}

解决方案

As I understand your question you want to set alarm at the click of a button. Use the below two methods to set alarm and cancel alarm. To repeat alarm use a unique requestcode in getBroadcast and when cancelling use the same requestcode value to cancel the alarm which was initially used to start it.

When the button is clicked call setAlarm method.

public void setAlarm(Calendar time,int pk) {
        AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent alarmIntent = new Intent(context, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, pk, alarmIntent, 0);
        manager.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent);
    }


public void cancelAlarm(int pk) {
        AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        Intent alarmIntent = new Intent(context, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, pk, alarmIntent, 0);
        manager.cancel(pendingIntent);
    }

这篇关于Android的设置多个重复闹钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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