0

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();
  • 1
    I don't know who told you you couldn't do a timed task repeat itself using a background service but it's wrong since i'am doing it for data refresh purposes in my own app. – N.K Apr 06 '18 at 07:55
  • Have you tried using the solution mentioned in the linked Q&A: "Use `setRepeating()` on Android 1.0-4.3 and `setExact()` on Android 4.4+. With `setExact()`, as part of processing one alarm event, you will need to schedule the next alarm event."? – user202729 Apr 06 '18 at 07:55
  • I must add, but to do that with a service you'll need to start it at least once when your user launch your app, then even if he decide to close the app the service should still be running in the background doing is job. On the other hand i'am unsure weither or not you can do that without launching your app at least once when the user restart is phone for example. – N.K Apr 06 '18 at 07:57
  • @user202729 I try setRepeting but i don't find some good example how do it with setExact(). – Erik Juríček Apr 06 '18 at 07:58
  • @xoxel I try it but in background without charging the service are paused. It have set repeat time 60s but do on 2,4,10,20 minutes. But only when the mobile phone are disconnect from charge. – Erik Juríček Apr 06 '18 at 07:59
  • Pretty weird, it can be related to some android system's behavior else it's your implementation of it that needs to be changed. For example facebook's messenger app works no matter if you have your phone on charge or not. I suggest you make search on this problem by looking at the doc, who know, you might find a good solution to your problem. – N.K Apr 06 '18 at 08:02
  • @xoxel Start application is not problem, I have application and i start service from button, but i need that service do everything every 60s. When i connect to charge everything is ok and service do everything 60s but when i discconect service started massive delay, on begun 2-4 minutes but after 10-20 minutes around 10-20minutes delay. – Erik Juríček Apr 06 '18 at 08:03
  • @xoxel Here is my service where i repeat some methode. – Erik Juríček Apr 06 '18 at 08:06
  • @pskink 15 is just tesiting time, i work with 1 min reload. but when it comes down to 15 seconds I'll be sure it'll go from 1 minute – Erik Juríček Apr 06 '18 at 08:08
  • @pskink I programing this for one game and i need to verify the data from their api. It must be up-to-date information to warn players of changes. – Erik Juríček Apr 06 '18 at 08:13

0 Answers0