Search code examples
androidmultithreadingandroid-asynctaskexecuterunnable

My async task does not work correctly, does not call execute


On my button i have the following code:

    Log.i(TAG, "!isFirstVideo: " + isFirstVideo);
ConcatenateVideos concatenateVideos = new ConcatenateVideos();
concatenateVideos.setUris(firstVideoUri, fileUri, VideoRecorderActivity.this);

This is my asynctask class:

public class ConcatenateVideos extends AsyncTask<String, Void, String> {

private String firstVideoUri, fileUri;
private VideoRecorderActivity videoRecorderActivity;

public void setUris(String firstVideoUri, String fileUri, VideoRecorderActivity videoRecorderActivity) {
    Log.i("VideoRecorderActivity", "set uris");
    this.firstVideoUri = firstVideoUri;
    this.fileUri = fileUri;
    this.videoRecorderActivity = videoRecorderActivity;
    this.execute();
    Log.i("VideoRecorderActivity", "set uris2");
}

@Override
protected String doInBackground(String... params) {
    Log.i("VideoRecorderActivity", "concat do in background");
    FileInputStream videoMain = null, videoToAppend = null;
    try {
        videoMain = new FileInputStream(firstVideoUri);
        videoToAppend = new FileInputStream(fileUri);
    } catch (FileNotFoundException e) {
        Log.i("VideoRecorderActivity", "concat file not found " + e);
        e.printStackTrace();
    }
    if (videoMain != null && videoToAppend != null) {
        concatenateVideos(videoMain, videoToAppend);
        videoRecorderActivity.deleteCancelledRecording(fileUri);
    }
    return null;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
    VideoRecorderActivity.recordButton.setEnabled(true);
    VideoRecorderActivity.okButton.setEnabled(true);
    VideoRecorderActivity.mySurfaceView.setEnabled(true);
    Toast.makeText(videoRecorderActivity, "Video appended succesfully", Toast.LENGTH_SHORT).show();
    Log.i("VideoRecorderActivity", "concat touche enabled record, ok ,surface: " + VideoRecorderActivity.recordButton.isEnabled() + "| " + VideoRecorderActivity.okButton.isEnabled() + "|" + VideoRecorderActivity.mySurfaceView.isEnabled());
}

In logcat i get:

04-11 12:21:42.520: I/VideoRecorderActivity(7927): set uris
04-11 12:21:42.520: I/VideoRecorderActivity(7927): set uris2

But i do not get Log.i("VideoRecorderActivity", "concat do in background"); that is the first line of the doInBackground class, that is called betweem those 2 lines:

Log.i("VideoRecorderActivity", "set uris");
this.firstVideoUri = firstVideoUri;
this.fileUri = fileUri;
this.videoRecorderActivity = videoRecorderActivity;
this.execute();
Log.i("VideoRecorderActivity", "set uris2");

Any ideeas what I am doing wrong?


Solution

  • When any service run in background for that activity. For Example , when music runs in background with service, the Async task does not parse XML in background as its doInBackground does not work that time and the progress Dialog or progressBar kept spinning. That is why I used Executor instead of old asynctask. The problem was that i have a download manager, that works on the background of another activity, and downloads all videos from a database, that do not exist local. And this working with the asynctask class, it could not execute this one.