简体   繁体   中英

alarm repeating at specific day not every day example ( Sunday, Tuesday , Friday)

I am tired to find specific day about alarm and always run every day pleas who can help me AND use these code what error about this ,run every so i send dayOfWeek=1; but run every day

 public void setAlarm(int dayOfWeek) {
     Toast.makeText(getApplicationContext(), dayOfWeek+","+h+","+m, 22222).show();
     cal1.set(Calendar.DAY_OF_WEEK, dayOfWeek);
     cal1.set(Calendar.HOUR, 11);
        cal1.set(Calendar.MINUTE, 0);
        cal1.set(Calendar.SECOND, 0);
        cal1.set(Calendar.MILLISECOND, 0);
        Intent intent = new Intent(this, RemmemberActivity.class);
        PendingIntent pendingIntent0 = PendingIntent.getBroadcast(this, 0,
                intent, 0);
        pendingIntent    = PendingIntent.getActivity(this, 12345,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);

            Long alarmTime = cal1.getTimeInMillis();
            am   = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
        am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime,24 * 60 * 60 * 1000 , pendingIntent); 
}

After what I can see after a quick look you set alarm to repeat every 24 hours on this row:

am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime,24 * 60 * 60 * 1000 , pendingIntent);

Your dayOfWeek is only used for when it will go off the first time.

If you have an alarm that should go off three times a week on the same time, make three alarms that repeat once a week.

am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, 7 * 24 * 60 * 60 * 1000 , pendingIntent);

finally this right solution if set as (sun,tus ,fri) you must create three alarm for these three day the following code set alarm every sunday and send dayOfWeek=1; important note every create a run at some day must change request code in every intnet (12345) in example

 public void setAlarm_sun(int dayOfWeek) {
     cal1.set(Calendar.DAY_OF_WEEK, dayOfWeek);
     Toast.makeText(getApplicationContext(), "sun "+cal1.get(Calendar.DAY_OF_WEEK), 222).show();

     Toast.makeText(getApplicationContext(), "Finsh", 222).show();

        Intent intent = new Intent(this, SecActivity.class);
        PendingIntent pendingIntent0 = PendingIntent.getBroadcast(this, 0,
                intent, 0);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 12345,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);

         Long alarmTime = cal1.getTimeInMillis();
         AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);

       // am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime,7* 24 * 60 * 60 * 1000 , pendingIntent);
        am.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime,7* 24 * 60 * 60 * 1000 , pendingIntent);

} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM