android doinbackground toast,android – 如何调试AsyncTask的doInBackground代码

  • Post author:
  • Post category:其他


我有断点设置,但是似乎是忽略(或从未见过).

我的代码如下我试图将一个sql数据库备份到SD卡.

当我在eclipse中运行它(而不是调试模式)时,我从onPreExecute得到消息,并在短时间内通过onPostExecute的消息.

我已经在ExportDatabaseFileTask的几乎每一行都设置了BreakPoints.

当我在eclipse中运行(在调试模式下)时,我停止在onPreExecute的断点处,然后当我进一步调试时,Debugger所做的很多NEXT LINE是:

mDbHelper.open();

然后我继续阅读正常代码的其余部分,然后放在AVD上,显示从onPreExecute的消息,它将显然保持“永远”.

我没有看到BREAKPOINTED中的任何一行:

doInBackground

onPostExecute

复制文件

所以我必须尊重不同意我没有被断定或不被执行的评论.所以,我会回答我的问题:你如何调试(Step THROUGH)一个AsyncTask的doInBackground代码?

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 {

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

}

}

}

}