Fire notification at every 24 hours and at exact time of 8 AM

后端 未结 4 1002
再見小時候
再見小時候 2020-12-08 02:39

I am using AlarmManager() to fire notification and repeat it at every 24 hours.

My code is on onCreate() in Splash Activity which fires fi

相关标签:
4条回答
  • 2020-12-08 03:18

    Here is pseudocode :

    You have to write this code inside your Splash.java

    Step 1 : Is_Alarm_Set(); [Boolean]

    Step 2 : false [step 3]

    Step 2 : true [step 8] (No need to set)

    Step 3 : Get_Time() [User's Current Mobile Time]

    Step 4 : Find_Time_Difference() [This function will find difference between user's mobile time and your Fix Time (8AM).]

    Step 5 : Now set your alarm as per time difference.[i.e current time is 7 pm and date is 1-june then set alarm of 8 AM for next day.]

    Step 6 : Set your repetition days setRepeating()

    Step 7 : It will fire alarm as per your fix time 8 AM.

    Step 8 : Switch Your Activity.

    0 讨论(0)
  • 2020-12-08 03:28

    Appu answer helped me a lot but if you want a fixed version of it a bit more readable and shorted look at this:

    PendingIntent pendingIntent = PendingIntent.getBroadcast(Splash.this, 0, myIntent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    Calendar firingCal= Calendar.getInstance();
    Calendar currentCal = Calendar.getInstance();
    
    firingCal.set(Calendar.HOUR_OF_DAY, 8); // At the hour you wanna fire
    firingCal.set(Calendar.MINUTE, 0); // Particular minute
    firingCal.set(Calendar.SECOND, 0); // particular second
    
    if(firingCal.compareTo(currentCal) < 0) { 
       firingCal.add(Calendar.DAY_OF_MONTH, 1);
    } 
    Long intendedTime = firingCal.getTimeInMillis();
    alarmManager.setRepeating(AlarmManager.RTC, intendedTime , AlarmManager.INTERVAL_DAY, pendingIntent);
    
    0 讨论(0)
  • 2020-12-08 03:43
    // Retrieve a PendingIntent that will perform a broadcast
                Intent alarmIntent = new Intent(HomeContactActivity.this,
                        AlarmReceiver.class);
                pendingIntent = PendingIntent.getBroadcast(
                        HomeContactActivity.this, 0, alarmIntent, 0);
    
                AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    
                // Set the alarm to start at 10:00 AM
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                calendar.set(Calendar.HOUR_OF_DAY, 10);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
    
    
    
    
                manager.setRepeating(AlarmManager.RTC_WAKEUP,
                        calendar.getTimeInMillis(), 86400000, // for repeating
                                                                // in every 24
                                                                // hours
                        pendingIntent);
    
    0 讨论(0)
  • 2020-12-08 03:44

    Do as chintan suggested. To get a clear picture, the exact solution might look something similar to the below:

    Intent myIntent = new Intent(Splash.this, AlarmReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(Splash.this, 0, myIntent, 0);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    
    Calendar firingCal= Calendar.getInstance();
    Calendar currentCal = Calendar.getInstance();
    
    firingCal.set(Calendar.HOUR, 8); // At the hour you wanna fire
    firingCal.set(Calendar.MINUTE, 0); // Particular minute
    firingCal.set(Calendar.SECOND, 0); // particular second
    
    long intendedTime = firingCal.getTimeInMillis();
    long currentTime = currentCal.getTimeInMillis();
    
    if(intendedTime >= currentTime){ 
       // you can add buffer time too here to ignore some small differences in milliseconds
       // set from today
       alarmManager.setRepeating(AlarmManager.RTC, intendedTime, AlarmManager.INTERVAL_DAY, pendingIntent);
    } else{
       // set from next day
       // you might consider using calendar.add() for adding one day to the current day
       firingCal.add(Calendar.DAY_OF_MONTH, 1);
       intendedTime = firingCal.getTimeInMillis();
    
       alarmManager.setRepeating(AlarmManager.RTC, intendedTime, AlarmManager.INTERVAL_DAY, pendingIntent);
    }
    
    0 讨论(0)
提交回复
热议问题