AlarmManager setInexactRepeating, setWindow, setRepeating methods do not fire alarm when called from within loop for week days

前端 未结 1 1943
再見小時候
再見小時候 2020-12-02 02:49

Requirement :-
I need to fire alarm on selected days of a week and a date from which alarms would start(for example I want to fire alarm on Friday and Saturday of every

相关标签:
1条回答
  • 2020-12-02 03:33

    try this code its working for Friday Alarm and similarly you can set for Saturday First you have to register your alarm Receiver and alarm time

    public static void SetAlarmForFriday(Context mContext) {
            try {
    
                Calendar calendar = Calendar.getInstance();
                calendar.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
                System.out.println("Date " + calendar.getTime());
    
                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH) + 1;
                int day = calendar.get(Calendar.DAY_OF_MONTH);
    
                String DateToConvert = day + "," + month + "," + year + " 10:10 AM";
                Date date = new SimpleDateFormat("dd,MM,yyyy hh:mm a", Locale.ENGLISH).parse(DateToConvert);
    
                Intent intent = new Intent(mContext, AlarmReceiver.class);
                intent.setAction("setYourActionHere");
                PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext.getApplicationContext(), 234324243, intent, 0);
                AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(mContext.ALARM_SERVICE);
    
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, date.getTime(), AlarmManager.INTERVAL_DAY, pendingIntent);
    
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    

    Make Receiver Class

    public class AlarmReceiver extends BroadcastReceiver {
        public AlarmReceiver() {
        }
    
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO: This method is called when the BroadcastReceiver is receiving
            // an Intent broadcast.
    
    
        }
    
    }
    

    Register your Broadcast receiver in manifest file under Application tag

    <receiver
                android:name=".AlarmReceiver"
                android:enabled="true"
                android:exported="true"></receiver>
    
    0 讨论(0)
提交回复
热议问题