0
 AlarmManager manager = (AlarmManager)MainActivity.this.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(MainActivity.this, DemoReceiver.class);
   PendingIntent     pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    manager.setInexactRepeating(AlarmManager.RTC, 0, 20000, pendingIntent);

i set 20 sec time interval to call alarm but it taking 1-1.10 min to call

  • Notice the `Inexact` part of that method name. Also, if you need an interval that short, you shouldn't be using `AlarmManager`. – Mike M. Jun 02 '16 at 05:30

2 Answers2

0

Try this

manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 0, 20000, pendingIntent);
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
S Haque
  • 6,881
  • 5
  • 29
  • 35
0

Which android version are you using ?

Apparently you cannot set very short intervals in Android AlarmManager.

Here - scheduling alarm for every second in android 5.1

Community
  • 1
  • 1
Akshat
  • 278
  • 5
  • 19
  • my min sdk is 18 and i removed the targated SDk – prashant date Jun 02 '16 at 06:44
  • Short intervals are not supported in Android AlarmManager. You can use Executors or TimerTask. Please refer to the link I shared. – Akshat Jun 02 '16 at 06:50
  • does the Executors or TimerTask works if my app in background or i removed app from current running apps – prashant date Jun 02 '16 at 06:52
  • Unfortunately you do not have complete control over TimerTask and Executors. Android OS may at any point of time clear the memory i.e. remove your tasks to accommodate another process. But in AlaramManager it is different, it can wake up and run a service as you code it BUT if the application is killed by the user then even the AlarmManager fails. So both of them have their pros and cons. Choose wisely. – Akshat Jun 02 '16 at 08:58
  • If this is the answer to your question, please accept it. It will help other devs to understand this scenario better. – Akshat Jun 02 '16 at 19:31
  • this is not what i want, – prashant date Jun 03 '16 at 10:45