This is one of the most strange cases I saw.
I have an application that performs some task every 5 minutes. I set repeating alarm using the following code:
public static final int INTERVAL_SECONDS = 300;
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, System.currentTimeMillis(), 1000 * INTERVAL_SECONDS, pi); // Millisec * Second * Minute
}
So, every five minutes I expect that the following code will run:
@Override
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 pper = new Intent(context, PushPullService.class);
context.startService(pper);
Log.d("AlarmReceiver", "Called context.startService from AlarmReceiver.onReceive");
wl.release();
}
My app has the following permissions in the manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
I see very strange things. I added writing to log file at the beginning of the intent. I saw that there is strange pattern:
There are events (printing to the log at the beginning of the intent procedure) every 5 minutes, but sometimes I see nothing in the log file for some time period, and then I see many events at once. The number of the events that run together (one after the other) seems to be the number of events I would expect to appear during the time period where no event was fired.
Any advice? What could it be? I didn't find any evidence to such behaviour. Maybe it is something in my own device?
EDIT: I added a print to the log when the broadcast receiver was fired. I saw that the broadcast is received every five minutes, but the intent is not being run!
I also added a unique id to each intent, as was offered by Irfan Ahmed, but it didn't solve the problem:
Intent i = new Intent(context, Alarm.class);
i.setData(Uri.parse(String.valueOf(System.currentTimeMillis())));
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, Intent.FLAG_GRANT_READ_URI_PERMISSION);
you need to set a unique id for every alarm here..
first, check your manifest, it looks like this,
Then, in your setAlarm method,
Generate uniqueId from timestamp or what,
EDIT
When you set alarms using,
this actually asign that pendingIntent to OS, and you are adding 300 seconds to the current time, then exactly after five minutes, your on receive call and you recieve notification...