5

i want to make a service which fire alarm manager in every 5 min interval when my application is running only..so how to do it?

 Intent intent = new Intent(this, OnetimeAlarmReceiver.class);
  PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, 0);

  AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
  alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (5 * 1000), sender);
   Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
shyam
  • 1,276
  • 4
  • 25
  • 51
  • 1
    Why do you need a service? You do NOT need a service just to have an alarm going off every 5 minutes. – Mike dg Oct 07 '11 at 14:37

4 Answers4

9

try this:

alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 5*60*1000, pendingIntent);

that alarm will be repeating forever until you cancel it, so you need to cancel it on getting the event when you no longer need it.

Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
2
private class ProgressTimerTask extends TimerTask {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // set your time here
                    int currenSeconds = 0
                    fireAlarm(currenSeconds);
                }
            });
        }
    }

Inizialize :

     Timer progressTimer = new Timer();
     ProgressTimerTask   timeTask = new ProgressTimerTask();
     progressTimer.scheduleAtFixedRate(timeTask, 0, 1000);
Yahor10
  • 2,123
  • 1
  • 13
  • 13
  • wont work in service check this http://stackoverflow.com/questions/14924295/service-of-an-app-stops-when-phone-is-not-being-charged for the reason why – laplasz Feb 21 '13 at 21:07
2

Cancel the AlarmManager in onPause() of the activity.

A much better solution would be to use a Handler with postDelayed(Runnable r, 5000) since you said only when your application is running. Handlers are much more efficient than using AlarmManager for this.

Handler myHandler = new Handler();
Runnable myRunnable = new Runnable(){
   @Override
   public void run(){
      // code goes here
      myHandler.postDelayed(this, 5000);
   }
}

@Override
public void onCreate(Bundle icicle){
   super.onCreate(icicle);
   // code
   myHandler.postDelayed(myRunnable, 5000);
   // to start instantly can call myHandler.post(myRunnable); instead
   // more code
}

@Override
public void onPause(){
   super.onPause();
   // code
   myHandler.removeCallbacks(myRunnable); // cancels it
   // code
}
DeeV
  • 35,865
  • 9
  • 108
  • 95
  • wont work in service, check this http://stackoverflow.com/questions/14924295/service-of-an-app-stops-when-phone-is-not-being-charged) for the reason – laplasz Feb 21 '13 at 21:06
  • 2
    This wasn't meant to be run in a Service. He was asking how to call something every 5 minutes **only when the app is running**. Starting a Service would have been overkill as the other answers were saying. – DeeV Feb 21 '13 at 21:20
0

Set Alarm repeating every 5 minutes in Kotlin

val intent = Intent(context, OnetimeAlarmReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), (1000 * 60 * 5).toLong(), pendingIntent)

To cancel Alarm

        val intent = Intent(context, OnetimeAlarmReceiver::class.java)
        val sender = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE)
        val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        alarmManager.cancel(sender)
Thiago
  • 12,778
  • 14
  • 93
  • 110