Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run an android function every 15 minutes , and specifically on the 15 minute mark everyday?

This maybe a repeated question here but I'm still facing issues on this, hope there's a solution around. Thanks in advance.

How do I set the alarm manager to run so as to execute the function specifically at every 15 minutes clock interval, for example 8:00, 8:15, 8:30, 8:45, 9:00 everyday regardless of the time when the application is started.

Take an example if let's say I started my application main activity at 7:47, and I would like the alarm to start at 8:00, followed along with 8:15, 8:30 and etc....how should I do? I would really appreciate if any suggestions are given..

like image 640
XXX Avatar asked Jun 13 '16 11:06

XXX


2 Answers

you should setup two alarm manager.for the first one set trigger time by calculating the remain time to your appropriate time (for example 8:00 Am). and after that you should create another alarm manager inside of your first alarm manager that will trigger each 15 minutes.

for calculate time for set first alarm manager use the code bellow:

Calendar c = Calendar.getInstance(); 
int m = calendar.get(Calendar.MINUTE);
long start = System.currentTimeMillis();
int remain=0;
if (m<15)
{
 remain = 15-m;
}
else if (m<30)
{
 remain = 30-m;
}
else if (m<45)
{
 remain = 45-m;
}
else
{
 remain = 60-m;
}
remain=start + remain*60*1000// convert it to milisecond and plus it to current time;

Edit

and after that you can use code bellow to setup first alarm manager:

 AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, FirstReceiver.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
    am.set(AlarmManager.RTC_WAKEUP,remain, pi);

so now in your FirstReceiver class do something like bellow:

public class FirstReceiver extends BroadcastReceiver
{
//do what ever you want + code bellow to setup second alarm manager
 Intent intent = new Intent(this, SecAlarm.class);
 PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, 0);
 AlarmManager am=  (AlarmManager)getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME,SystemClock.elapsedRealtime(),
                15*60*60,pendingIntent);
}
like image 56
pouyan Avatar answered Oct 12 '22 23:10

pouyan


Better to set an alarm clock for every time you want with one day interval

public void setAlarm(Context context) {
    AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, Alarm.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    am.setRepeating(AlarmManager.RTC_WAKEUP, yourTimeInMilisecond, 24 * 60 * 60 * 1000, pi); // Millisec * Second * Minute
}

Here yourTimeInMillisecond is the time you want to set alarm, like if you want to set alarm for tomorrow 9 am then it will be :

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);       
calendar.add(Calendar.DAY_OF_YEAR, 1);
long yourTimeInMilisecond = calendar.getTimeInMillis();

Hope it will work for you :)

like image 44
Neo Avatar answered Oct 13 '22 00:10

Neo