user2440247
user2440247

Reputation:

Fire Notification at 10 AM of morning and at every 24 hours

I am using AlarmManager() to fire notification. But don't know how to set to fire at 10 AM of and at every 24 hours. Any one please help me.

My code is as below:

        Calendar Calendar_Object = Calendar.getInstance();
        Calendar_Object.set(Calendar.HOUR_OF_DAY, 10);
        Calendar_Object.set(Calendar.MINUTE, 00);
        Calendar_Object.set(Calendar.SECOND, 0);
        Intent myIntent = new Intent(Splash.this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(Splash.this, 0, myIntent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC,Calendar_Object.getTimeInMillis(),(24 * 60 * 60 * 1000),pendingIntent);

Upvotes: 0

Views: 793

Answers (2)

AnilPatel
AnilPatel

Reputation: 2366

 Calendar Calendar_Object = Calendar.getInstance();
        Calendar_Object.set(Calendar.HOUR_OF_DAY, 10);
        Calendar_Object.set(Calendar.MINUTE, 00);
        Calendar_Object.set(Calendar.SECOND, 0);
        Intent myIntent = new Intent(Splash.this, AlarmReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(Splash.this, 0, myIntent, 0);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC,Calendar_Object.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);

Upvotes: 0

amalBit
amalBit

Reputation: 12191

For every 24 hours try this code:

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
long now = System.currentTimeMillis();
long diffMillis = now - pref.getLong("CurrentTimeMillis", 0);
if( diffMillis >= (3600000  * 24) ) {

 // Your code here:)
SharedPreferences.Editor editor = pref.edit();
editor.putLong("CurrentTimeMillis", System.currentTimeMillis());
editor.commit();

} else {
 // too early
}

Upvotes: 1

Related Questions