Yesterday and today I tried to solve the problem from my service. I read lots of topics about this problem (when i want unstopable timer every 15s) but i don't find any solution.
First i try asynch task and service which are nevertheless implement WakeLock still pause when I disconnect the charger. In this topic i read that is no chance to do this in Service. (Here)
Now i try the AlertManager but i read that from some version of android don work repeting in small intervals.
My questions here is, How can i do this (15s interval which can be run always (sleep mode, no on charge etc..)) and android don't be stop this task. I need regular renewal every 15 seconds in all circumstances.
Someone help me how to do this? Thank
My service on create
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakelock= pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getCanonicalName());
wakelock.acquire();
runnable = new Runnable() {
public void run() {
while(true) {
if (!running) return;
if (updateVillages()) {
result = true;
if (isNewVillage()) {
if (isNotificationEnable) doNotificate();
}
System.out.println("Prebehlo overovanie");
} else {
result=false;
System.out.println("Není internetové pripojenie");
}
updateUI();
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
thread= new Thread(runnable);
thread.start();