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
- the service will compressing size of mp4 extention before sending (using FFMPeg)
- the service will delete the real video
- 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;
}
}