简体   繁体   中英

Alarm initiated automatically at first time set a alarm

I am using alarm to start service at every 15 days once. Alarm is initiated at the first time of installation. Every 15 days once alarm trigger that service properly, but At the time of initiating alarm the service started.I dont want to start the service at the time of installation. how to stop this. I dont whether i am using wrong format?

Note: Apart from this condition, I am not start service anywhere.

Intent intent = new Intent(context, InitiateService.class);
    PendingIntent pIntent = PendingIntent.getService(context, 12345, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 15 * AlarmManager.INTERVAL_DAY, pIntent);

this what i used in manifest file.

 <service android:name=".service.InitiateService" />

this is my code of initiating alarm for starting service at every 15days once.

Guys If any one found any wrong in my question, Apologies me and Please correct those mistakes and answer me.

it is because you used System.currentTimeMillis()

try this

System.currentTimeMillis()*(15 * AlarmManager.INTERVAL_DAY)

alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()*(15 * AlarmManager.INTERVAL_DAY), 15 * AlarmManager.INTERVAL_DAY, pIntent);

@Mehul Gajjar is correct about System.currentTimeMillis()

so you might wanna replace

System.currentTimeMillis()

with

(15 * 24 * 60 * 60 * 1000 * System.currentTimeMillis())

Explanation :

System.currentTimeMillis() will set Alarm to current time, so your service will start straight away. setting Alarm after 15 days will solve your problem. 15 * 24 * 60 * 60 * 1000 will be equals to 15 days. multiplying it with current time-miles will give you current time after 15 days.

in-short your code would be like

alarm.setRepeating(AlarmManager.RTC_WAKEUP, (15 * 24 * 60 * 60 * 1000 * System.currentTimeMillis()), 15 * AlarmManager.INTERVAL_DAY, pIntent); 

I hope this will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM