The setRepeating
method 2nd parameter suggests when the first alarm should go off. You have specified it as System.currentTimeMillis()
, hence it goes off immediately.
Change:
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), AlarmManager.INTERVAL_DAY*2, pendingIntent);
to
long updateInterval = AlarmManager.INTERVAL_DAY*2;
am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + updateInterval, updateInterval, pendingIntent);
Regarding your 2nd question
Secondly do I have to call the above code every time the user starts the app or I have to call it once only? Or I have to call it after two days? I want to send the notification every two days as long as the app is installed.
You will have to call it once. The notification will shown every two days until the app is uninstalled.
You can register a BroadcastReceiver
for intent android.intent.action.PACKAGE_FIRST_LAUNCH, which will be sent out the first time your application is launched. Here you can set the repeating alarm. May be you need to handle android.intent.action.PACKAGE_REPLACED, in case a newer version of your app is installed.