1

My Question is not duplicated as this question! my case is about AsyncTask not just about Runnable, and even so, I've 2 AsyncTask inside runnable that needed to handling!

I'm new in Android, I've an app created by React Native. but the problem is I want to upload any data (image [jpg] & video [mp4]) that stored in sqlite to server through rest-API and handling the task in android (Native Module)

Workflow :

  • My service need to keep running on background to check any data stored on sqlite
    1. the service will compressing size of mp4 extention before sending (using FFMPeg)
    2. the service will delete the real video
    3. the service will send the data into server (I add 1 column called flag to check if the video already compressed)

Problem :

I've create compressTask and sending function inside service, but the compressTask and sending using extends AsyncTask that mean if the compressTask already execute, sending function will execute too when compressTask not yet finished,

Example : I've 2 row of video in sqlite, the service will execute compressTask and already have 1 video compressed, but sending will executed before 2nd video already compressed

Expected Result : I want to handle sending to wait 2 video already compressed, How can I do that?

My code :

public class myService extends Service {
    public Runnable mRunnable = null;
    private boolean mRunning = false;
    Handler mHandler = new Handler();
    IBinder mBinder = new LocalBinder();
    String newPathOutputVideo = null;
    int idProcessVideo = 0;
    String log = null;
    @Inject
    FFmpeg ffmpeg;

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

    public class LocalBinder extends Binder {
        public myService getServerInstance() {
            return myService.this;
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        loadFFmpeg();
        Log.d("Service"," onstart kepanggil ga?");
        mRunnable = new Runnable() {
            @Override
            public void run() {
                /* ---New Code--- */
                Log.d("Service","SERVICE RUN");
                compressTask c = new compressTask();
                c.execute();

                sending a = new sending();
                a.execute();
            mHandler.postDelayed(mRunnable, 10 * 1000);
            }
        };
        mHandler.postDelayed(mRunnable, 10 * 1000);
        return START_STICKY;
    }
}
flix
  • 1,688
  • 3
  • 34
  • 64
  • Possible duplicate of [Waiting for a Runnable to complete before running another Runnable](https://stackoverflow.com/questions/8799373/waiting-for-a-runnable-to-complete-before-running-another-runnable) – Jozef Dochan Feb 09 '18 at 07:02
  • `sending function will execute too when compressTask not yet finished,`. No. That does not happen. The doInBackground()s of AsyncTasks are executed sequentially. – greenapps Feb 09 '18 at 11:06
  • 1
    You could start the send asynctask in the onPostExecute of the compress task. – greenapps Feb 09 '18 at 11:09
  • Just start your tasks in the sequence you want them to be executed. – greenapps Feb 09 '18 at 11:10
  • plus1 for `send asynctask in the onPostExecute of the compress task` idea, never try this, – flix Feb 09 '18 at 11:12

0 Answers0