android 每个星期一或每个星期二重复闹钟 [英] android repeat alarm for every monday or every tuesday

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

问题描述

我正在开发一个基于警报的应用程序,其中我必须根据用户输入在每个工作日(例如每个星期一、星期二、星期三)重复警报.我使用了这个片段

I am developing an alarm based application, in which i have to repeat the alarm for every weekday like (every monday, tuesday, wednesday) based on user input. I used this snippet

Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, alarmToSetInMilliSeconds, sender);

如果用户选择每个星期一意味着,我发现到下一个星期一日期多毫秒,我设置了一个闹钟,它工作正常,我如何在下一个星期一重复它,我想要一些想法来实现它,任何帮助受到赞赏.谢谢.

If user selects every monday means, i found the milliseconds more to next monday date and i set an alarm, and its working fine, how can i repeat it for other next monday, I want some idea to achieve it, any help are appreciated. Thanks.

推荐答案

这里是我的代码,设置闹钟在 4 号每个月重复

here is my code to set alarm to repeat every month in 4th

public void init(Context context) {

    Intent intent = new Intent(context, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    // Get the AlarmManager service
    alarmManager = (AlarmManager) context
            .getSystemService(Context.ALARM_SERVICE);
}   
public void createAlarmControler(Context context) {

    init(context);
    Calendar cal = Calendar.getInstance();

    cal.set(Calendar.DAY_OF_MONTH, 4);
    cal.set(Calendar.HOUR_OF_DAY, 10);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    Date now = new Date(System.currentTimeMillis());
    if (cal.getTime().before(now)) {
        cal.add(Calendar.MONTH, 1);
    }
    long firstTime = cal.getTime().getTime();

    alarmManager.cancel(pendingIntent);
    Log.e("", "firstTime: " + firstTime);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, firstTime, 1000 * 60
            * 60 * 24 * 30, pendingIntent);
}

public void cancelAlarmControler(Context context) {

    init(context);

    alarmManager.cancel(pendingIntent);
}

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

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