如果设置重复两天+,则AlarmManager setRepeating不起作用

时间:2016-01-17 10:10:33

标签: android notifications alarmmanager

面对这样的问题:

我试图在每个星期一和星期五的23:00设置重复,但重复的工作只在星期一。

PendingIntent在不同的requestCode中使用。为什么只在星期一触发警报和通知无法理解任何人有任何想法?

AlarmHelper:

public class AlarmHelper {

private static AlarmHelper instance;
private Context context;
private AlarmManager alarmManager;
Intent intent;




public static AlarmHelper getInstance() {
    if (instance == null) {
        instance = new AlarmHelper();
    }
    return instance;
}


public void init(Context context) {
    this.context = context;
    alarmManager = (AlarmManager) context.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
}   

public void setAlarm(int day,ModelTask task) {
if (day==Calendar.MONDAY) {
                forDay(2, task);

            }
if (day==Calendar.Friday {
                forDay(6, task);

       }
}


public void forDay(int day, ModelTask task) {

    Calendar  c = Calendar.getInstance();
    c.set(Calendar.DAY_OF_WEEK, day);
    c.set(Calendar.HOUR_OF_DAY, 23);
    c.set(Calendar.MINUTE, 00);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);



        Intent  intent = new Intent(context, AlarmReceiver.class);
        intent.putExtra("title", task.getTitle());
        intent.putExtra("time_stamp", task.getTimeStamp());
        intent.putExtra("color", task.getPriorityColor());            
        intent.putExtra("numberDay", day);


        PendingIntent pendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(),
                day, intent, PendingIntent.FLAG_UPDATE_CURRENT);



        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent);


    }
}

AlarmReceiver:

public class AlarmReceiver extends BroadcastReceiver {


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


    String title = intent.getStringExtra("title");        
    long timeStamp = intent.getLongExtra("time_stamp", 0);
    int color = intent.getIntExtra("color", 0);
    int numberDay = intent.getIntExtra("numberDay", 0);


    Intent resultIntent = new Intent(context, MainActivity.class);


    if (MyApplication.isActivityVisible()) {
        resultIntent = intent;
    }

    resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);


    PendingIntent pendingIntent = PendingIntent.getActivity(context, numberDay,
            resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); 


    Resources res = context.getResources();


    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setContentTitle(res.getString(R.string.app_name));
    builder.setContentText(title);
    builder.setColor(context.getResources().getColor(color));
    builder.setSmallIcon(R.drawable.ic_check_white_48dp);


    builder.setDefaults(Notification.DEFAULT_ALL);

    builder.setContentIntent(pendingIntent);


    Notification notification = builder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;



    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify((int) timeStamp, notification);

}
}

1 个答案:

答案 0 :(得分:0)

目前,您的闹钟管理员每7天重复一次。 您必须更改所需日期的间隔。 在这一行:

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
            c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendingIntent);

您必须在星期一更改间隔,如下所示:

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
            c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 4, pendingIntent);

并在周五将其设置为:

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
            c.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 3, pendingIntent);

希望我能提供帮助:)