如何调试AsyncTask的doInBackground代码 [英] How can I debug doInBackground code of an AsyncTask
问题描述
我的代码如下。我试图将一个sql数据库备份到SD卡。
当我在eclipse中运行它(不是调试模式)时,我从onPreExecute获取消息,并且稍后从onPostExecute发出消息。
我在ExportDatabaseFileTask的几乎每一行都设置了BreakPoints。
当我运行它(在调试模式)在eclipse中,我停止在onPreExecute的断点,然后当我进一步,调试器去的非常NEXT LINE是:
mDbHelper.open();
然后,我将继续阅读其余的正常代码,并将AVD显示在onPreExecute上的消息中,它将显然是STAY FOREVER。
我没有看到任何BREAKPOINTED中的行:
doInBackground
onPostExecute
copyFile
所以,我必须尊重不同意我没有断点或没有被执行的评论。所以,我会回答我的问题:如何调试(步骤)AsyncTask的doInBackground代码?
case BACKUP_ID:
if(ScoreAGame.this.isExternalStorageAvail()){
mDbHelper.close();
new ExportDatabaseFileTask()。execute();
mDbHelper.open();
} else {
Toast.makeText(ScoreAGame.this,外部存储不可用,无法导出数据,
Toast.LENGTH_SHORT).show();
}
返回true;
case RESTORE_ID:
selectCourse();
返回true;
}
返回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);
//可以在这里使用UI线程
protected void onPreExecute(){
this.dialog.setMessage(Exporting database ...);
this.dialog.show();
}
//在工作线程上自动完成(与UI线程分开)
protected Boolean doInBackground(final String ... args){
文件dbFile =
new File(Environment.getDataDirectory()+/data/com.discgolf/databases/discgolfdb.db);
文件exportDir = new File(Environment.getExternalStorageDirectory(),discgolfbackup);
if(!exportDir.exists()){
exportDir.mkdirs();
}
文件文件= new File(exportDir,dbFile.getName());
try {
file.createNewFile();
this.copyFile(dbFile,file);
返回true;
}
catch(IOException e){
Log.e(TAG,e.getMessage(),e);
返回false;
}
}
//可以在这里使用UI线程
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,导出失败,Toast.LENGTH_SHORT).show();
}
}
void copyFile(File src,File dst)throws IOException {
FileChannel inChannel = new FileInputStream(src).getChannel();
FileChannel outChannel = new FileOutputStream(dst).getChannel();
尝试{
inChannel.transferTo(0,inChannel.size(),outChannel);
}
finally {
if(inChannel!= null){
inChannel.close();
}
if(outChannel!= null){
outChannel.close();
}
}
}
}
查看这个在开发Android时,如何在AsyncTask中使用Eclipse调试器?
将以下代码片段放在开头的doInBackground()
if(android.os.Debug.isDebuggerConnected())
android.os.Debug.waitForDebugger();
然后当您在该线程中设置断点时,eclipse会找到它。
礼貌:sargas
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();
}
}
}
}
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
这篇关于如何调试AsyncTask的doInBackground代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!