为每周一和每周二的android重复报警 [英] android repeat alarm for every monday or every tuesday

查看:155
本文介绍了为每周一和每周二的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);

如果用户选择每周一指,我发现毫秒以上至下星期一日期和我设置闹钟,它的做工精细,我怎么能重复用于其他下周一,我想要一些想法去实现它,任何帮助AP是preciated。谢谢。

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.

推荐答案

这是我的code设置报警每月重复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天全站免登陆