Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I debug doInBackground code of an AsyncTask

I have breakpoints set, but they seem to be ignore (or never seen).

My code is below. I'm trying to backup a sql db to an SD card.

When I RUN it (not Debug mode) in eclipse, I get the message from the onPreExecute and followed shortly by the message from the onPostExecute.

I have BreakPoints set in almost every line of the ExportDatabaseFileTask.

When I RUN it (in Debug mode) in eclipse, I stop at the breakpoints in onPreExecute, and then as I step further, THE VERY NEXT LINE the Debugger goes to is:

mDbHelper.open();

I then step through the rest of the normal code and am left wth the AVD showing the message from the onPreExecute, wher it will apparently STAY FOREVER.

I do NOT see any of the lines BREAKPOINTED in:

doInBackground onPostExecute copyFile

So, I have to respectfully disagree with the comment that I don't have it breakpointed or it is not being executed. So, I'll reask my question: How do you debug (STEP THROUGH) doInBackground code of an AsyncTask?

        case BACKUP_ID:
            if (ScoreAGame.this.isExternalStorageAvail()) {
                mDbHelper.close();
                new ExportDatabaseFileTask().execute();
                mDbHelper.open();
            } else {
                Toast.makeText(ScoreAGame.this, "External storage is not available, unable to export data.",
                           Toast.LENGTH_SHORT).show();
            }
            return true;
        case RESTORE_ID:
            selectCourse();
            return true;
    }

    return super.onMenuItemSelected(featureId, item);
}

private boolean isExternalStorageAvail() {
    return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}

private class ExportDatabaseFileTask extends AsyncTask<String, Void, Boolean> {
    private final ProgressDialog dialog = new ProgressDialog(ScoreAGame.this);

    // can use UI thread here
    protected void onPreExecute() {
        this.dialog.setMessage("Exporting database...");
        this.dialog.show();
    }

    // automatically done on worker thread (separate from UI thread)
    protected Boolean doInBackground(final String... args) {

        File dbFile =
            new File(Environment.getDataDirectory() + "/data/com.discgolf/databases/discgolfdb.db");

        File exportDir = new File(Environment.getExternalStorageDirectory(), "discgolfbackup");
        if (!exportDir.exists()) {
            exportDir.mkdirs();
        }
        File file = new File(exportDir, dbFile.getName());

        try {
            file.createNewFile();
            this.copyFile(dbFile, file);
            return true;
        }
        catch (IOException e) {
            Log.e(TAG, e.getMessage(), e);
            return false;
        }
    }

    // can use UI thread here
    protected void onPostExecute(final Boolean success) {
        if (this.dialog.isShowing()) {
            this.dialog.dismiss();
        }
        if (success) {
            Toast.makeText(ScoreAGame.this, "Export successful!", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(ScoreAGame.this, "Export failed", Toast.LENGTH_SHORT).show();
        }
    }

    void copyFile(File src, File dst) throws IOException {
        FileChannel inChannel = new FileInputStream(src).getChannel();
        FileChannel outChannel = new FileOutputStream(dst).getChannel();
        try {
            inChannel.transferTo(0, inChannel.size(), outChannel);
        }
        finally {
            if (inChannel != null) {
                inChannel.close();
            }
            if (outChannel != null) {
                outChannel.close();
            }
        }
    }

}

like image 762
Tom Wruble Avatar asked Aug 13 '11 14:08

Tom Wruble


People also ask

What is the primary reason to use the doInBackground method of an AsyncTask?

doInBackground(Params) – This method is invoked on the background thread immediately after onPreExecute() finishes its execution. Main purpose of this method is to perform the background operations that can take a long time. The parameters of the Asynchronous task are passed to this step for execution.

How many times an instance of AsyncTask can be executed?

AsyncTask instances can only be used one time.

When AsyncTask is executed it goes through what steps?

An asynchronous task is defined by 3 generic types, called Params , Progress and Result , and 4 steps, called onPreExecute , doInBackground , onProgressUpdate and onPostExecute .

What method must be overridden for using AsyncTask?

Usage. AsyncTask must be subclassed to be used. The subclass will override at least one method ( doInBackground(Params...) ), and most often will override a second one ( onPostExecute(Result) .)


1 Answers

check this How do I use the Eclipse debugger in an AsyncTask when developing for Android?

Put the following code fragment in the beginning of doInBackground()

if(android.os.Debug.isDebuggerConnected())
    android.os.Debug.waitForDebugger();

Then when you set a breakpoint in that thread, eclipse will find it.

Courtesy: sargas

like image 163
Imon Avatar answered Sep 27 '22 19:09

Imon