Question

I need to set 15 minutes alarm in my program. How do I set one?

This is the code I have so far:

public void onReceive(Context context, Intent intent) 
{   
   PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
   PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
   wl.acquire();

   Intent App = new Intent(context, LoginActivity.class);
   App.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   context.startActivity(App);

   Toast.makeText(context, "instantmeter waking restart", Toast.LENGTH_LONG).show(); // For example
   wl.release();
}

public void SetAlarm(Context context)
{
   AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
   Intent i = new Intent(context, PollReceiver.class);
   PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
   am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 * 100, pi); // Millisec * Second * Minute
}
Was it helpful?

Solution

Your code is correct but the Time Interval is incorrect. AlarmManager accepts value in miliseconds. So for 15 minutes, you need to calculate like below,

15 * 60 * 1000

Try this code,

public void onReceive(Context context, Intent intent) 
{   
   PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
   PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
   wl.acquire();

   Intent App = new Intent(context, LoginActivity.class);
   App.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   context.startActivity(App);

   Toast.makeText(context, "instantmeter waking restart", Toast.LENGTH_LONG).show(); // For example
   wl.release();
}

public void SetAlarm(Context context)
{
   AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
   Intent i = new Intent(context, PollReceiver.class);
   PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
   am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 15 * 1000 * 60 , pi); // Millisec * Second * Minute
}

OTHER TIPS

I have used the below code and it works:

public class AlarmManagerBroadcastReceiver extends BroadcastReceiver  
{
  private static final int _REFRESH_INTERVAL = 60 * 15; // 15 minutes


  public void setAlarm()
  {
    AlarmManager am=(AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
    pi = PendingIntent.getBroadcast(context, ALARM_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    try
    {
        am.cancel(pi);
    }
    catch (Exception ignored){}
    am.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime(), 1000 * _REFRESH_INTERVAL , pi);
  }

  @Override
  public void onReceive(Context context, Intent intent) 
  {
    // Alarm action here
  }
}

Guess the SystemClock.elapsedRealtime() would do the trick..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top