My service is intened to start every 15 minutes, however it starts again as soon as it was finished (on real HTC one mini). In same time that service is not repeated on emulated devices. In my activity I use following method to scheldue alarm, which starts service
private void enableService() {
SharedPreferences times = PreferenceManager
.getDefaultSharedPreferences(this);
LastUpdateConstructor.INSTANCE.setTotalSeconds(times.getLong(
"constructor", 0));
LastUpdatePopular.INSTANCE.setTotalSeconds(times.getLong("popular", 0));
LastUpdateActual.INSTANCE.setTotalSeconds(times.getLong("actual", 0));
LastUpdateVypusk.INSTANCE.setTotalSeconds(times.getLong("vypusk", 0));
// startService(new Intent(this, CheckForUpdateService.class));
boolean alarmUp = (PendingIntent.getService(this, R.string.app_name,
new Intent(SplashScreenActivity.this, CheckForUpdateService.class),
PendingIntent.FLAG_NO_CREATE) != null);
if (!alarmUp) {
Log.e("new alarm", "was atached");
Intent intent = new Intent(SplashScreenActivity.this, CheckForUpdateService.class);
PendingIntent pintent = PendingIntent.getService(SplashScreenActivity.this,
R.string.app_name, intent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,AlarmManager.INTERVAL_FIFTEEN_MINUTES,AlarmManager.INTERVAL_FIFTEEN_MINUTES, pintent);
}
}
Service is following
public class CheckForUpdateService extends Service {
private WakeLock mWakeLock;
@Override
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate() {
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
checkForUpdate();
}
public int onStartCommand(Intent intent, int flags, int startId) {
checkForUpdate();
return START_NOT_STICKY;
}
public Runnable check = new Runnable() {
@Override
public void run() {
checkForUpdate();
}
};
public void checkForUpdate() {
Log.e("alarm", "alarm set");
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "predanie");
mWakeLock.acquire();
new GetAdds().execute();
}
public void onDestroy(){
super.onDestroy();
mWakeLock.release();
}
private class GetAdds extends AsyncTask<Void, Void, String> {
@Override
protected void onPreExecute() {
}
@Override
protected String doInBackground(Void... params) {
String message = "";
String vypusk = checkVypusk();
String popular = checkPopular();
String actual = checkActual();
String constructor = checkConstructor();
message += vypusk;
message += constructor;
message += popular;
message += actual;
return message;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (!result.equals("")) {
postMessage(result);
Log.e("notification alarm", "alarm was fiered");
} else {
Log.e("notification alarm", "alarm was triggered , but empty");
}
stopSelf();
}
}
}
I also tried using calendar as someone noe my alarme time is set in past
private void enableService() {
SharedPreferences times = PreferenceManager
.getDefaultSharedPreferences(this);
LastUpdateConstructor.INSTANCE.setTotalSeconds(times.getLong(
"constructor", 0));
LastUpdatePopular.INSTANCE.setTotalSeconds(times.getLong("popular", 0));
LastUpdateActual.INSTANCE.setTotalSeconds(times.getLong("actual", 0));
LastUpdateVypusk.INSTANCE.setTotalSeconds(times.getLong("vypusk", 0));
// startService(new Intent(this, CheckForUpdateService.class));
boolean alarmUp = (PendingIntent.getService(this, R.string.app_name,
new Intent(SplashScreenActivity.this,
CheckForUpdateService.class),
PendingIntent.FLAG_NO_CREATE) != null);
if (!alarmUp) {
Log.e("new alarm", "was atached");
Intent intent = new Intent(SplashScreenActivity.this,
CheckForUpdateService.class);
PendingIntent pintent = PendingIntent.getService(
SplashScreenActivity.this, R.string.app_name, intent, 0);
AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.MINUTE, 2);
alarm.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(),
5*60*1000, pintent);
}
}
It seems this problem was cause by some kind of bug. After i disconnected device from computer and rebooted it, services were called in desiered interval