How can I debug doInBackground code of an AsyncTask

19,057

Solution 1

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

Solution 2

If they are ignored, then it's probably one of this two:

  1. You run the project not in debug mode.
  2. You never really reach those lines during execution.

Solution 3

In your doInBackground() add the following..`

    protected Integer doInBackground(String... params) {
    // for debug worker thread
    if(android.os.Debug.isDebuggerConnected())
        android.os.Debug.waitForDebugger();

    // create the file with the given file path
    File file = new File(params[0]);

    // enter rest of the code here..
}

`

Solution 4

I was facing something similar in Android Studio. For me, it was due to Proguard being active during debug. Proguard was messing up the line numbers after minifying. You can disable Proguard by removing minfyEnabled true from your build.gradle

Solution 5

If another Async Task is running in the background, then may be new one skip to run doinbackground method, so try to run like:

(new ExampleAsyncTask()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
Share:
19,057
Tom Wruble
Author by

Tom Wruble

Updated on June 13, 2022

Comments

  • Tom Wruble
    Tom Wruble almost 2 years

    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();
                }
            }
        }
    

    }

  • Aloha
    Aloha about 6 years
    Can't believe I forgot .execute(). +1