-1

What's the best approach on Android to create a recurrent thread that get data from Web Api and notify user when event happen?

I was using a Alarm Manager that call an Intent Service but looks like this is not the best. It dies when the system is rebooted.

Does anybody have a good reccomandation?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
peterdest
  • 285
  • 3
  • 11

2 Answers2

0
while(1>0){
    Result r = getResultFromAPI(args);
    processResults(r);
    Thread.sleep(sleepTimeInMillis);
}

also if you have access to modify the service you can set it up to do push notifications.

mavriksc
  • 1,130
  • 1
  • 7
  • 10
0

This can be implemented via Android Service.

Benefits of Android Service with START_STICKY

  • Android will take responsibility to start your service again if stopped by any reason.
  • Below service runs on worker thread which will never hang your app.
  • get data api will never be call twice if in process. (flag maintained)

I implemented this class in my location tracking app. which you can just paste and use.

package in.kpis.tracker.projectClasses.services;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


/**
 * Created by KHEMRAJ on 1/24/2018.
 */

public class SyncLocationServiceSample extends Service {
    public String TAG = "SyncLocationService";
    private Thread mThread;
    ScheduledExecutorService worker;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand");
        super.onStartCommand(intent, flags, startId);
        return START_STICKY;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (worker == null) worker = Executors.newSingleThreadScheduledExecutor();
        if (mThread == null || !mThread.isAlive()) {
            mThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    syncLocationAPI();
                    if (worker != null) {
   // 60 * 1000 is 60 second, change it as your requirement.
                        worker.schedule(this, 60 * 1000, TimeUnit.MILLISECONDS);
                    }
                }
            });
            mThread.start();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopThread();
    }

    boolean apiCalling = false;

    private synchronized void syncLocationAPI() {
        if (apiCalling) return;
//        do your api call here.
// make apiCalling true when you call api, and apiCalling  false when response come
    }

    private void stopThread() {
        worker = null;
        if (mThread != null && mThread.isAlive()) mThread.interrupt();
    }
}

Things to be consider

You will start this service with an device boot complete receiver. How to make boot complete receiver. Let me know if you need help for starting service.

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • My solution is actually similar. I just have an IntentService that use internally an AlarmManager to wake up. But it crashes when I try to start the service from the boot. I think that is it because of Android Oreo, but I can't find any solution – peterdest Apr 13 '18 at 19:18
  • Dude IntentService is not reliable for this type of requirement – Khemraj Sharma Apr 13 '18 at 19:19
  • Use this service and let me know if you find any issue. Because this is working well in my location tracking app, which needs a reliable service. – Khemraj Sharma Apr 13 '18 at 19:20
  • you can not start intent service as start sticky, for long thread above service is recommended – Khemraj Sharma Apr 13 '18 at 19:36
  • it works, the problem was actually with android O that basically want a Foreground Service. But anyway I appreciated your solution – peterdest Apr 17 '18 at 01:23