0

I have find any similar question about my problem a couple hours ago, but i'm not find it yet,

I want to upload data using Service with Handler and Runnable but I facing the problem that handler.postDelayed(myRunnable, interval) is re-run when the block code inside run() is not yet finish, all i know is to use wait() and notify() but i don't know where should i put these methods

my Service code looks like:

public class ServiceUploadData extends Service {
  Handler handler = new Handler();
  private Runnable periodicUpdate = new Runnable() {
    @Override
    public void run() {
      Log.v(TAG, "Service Upload Data Running Now");
      //check localDB and send the data if exist
      handler.postDelayed(this, 1000);
    }
  };

  @Override
  public int onStartCommand(Intent intent, int flags, int startId)
  {
    Log.v(TAG, "onStartCommand");
    context = this;
    handler.post(periodicUpdate);
    return START_STICKY;
  }
  //any @Override
}

anyone can help me out?

2
  • May I ask a stupid question: is onStartCommand called multiple times? Jan 31, 2019 at 13:59
  • No. onStartCommand just called once, but the interval not sync
    – flix
    Jan 31, 2019 at 14:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.