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

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

我的代码如下我试图将一个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();

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
欢迎!以下是一个简单的Android下载文件的代码示例: 首先,在AndroidManifest.xml文件中添加网络权限: ```xml <uses-permission android:name="android.permission.INTERNET" /> ``` 然后,在Activity中创建一个异步任务类来处理下载: ```java import android.app.ProgressDialog; import android.os.AsyncTask; import android.os.Environment; import android.util.Log; import android.widget.Toast; import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class DownloadTask extends AsyncTask<String, Integer, String> { private ProgressDialog progressDialog; @Override protected void onPreExecute() { super.onPreExecute(); progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setTitle("下载文件"); progressDialog.setMessage("正在下载,请稍候..."); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.show(); } @Override protected String doInBackground(String... params) { String fileUrl = params[0]; String savePath = Environment.getExternalStorageDirectory().getPath() + "/downloaded_file.jpg"; try { URL url = new URL(fileUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.connect(); int fileLength = connection.getContentLength(); InputStream inputStream = connection.getInputStream(); FileOutputStream fileOutputStream = new FileOutputStream(savePath); byte[] buffer = new byte[1024]; int bytesRead; long totalBytesRead = 0; while ((bytesRead = inputStream.read(buffer)) != -1) { totalBytesRead += bytesRead; publishProgress((int) ((totalBytesRead * 100) / fileLength)); fileOutputStream.write(buffer, 0, bytesRead); } fileOutputStream.close(); inputStream.close(); connection.disconnect(); } catch (Exception e) { Log.e("DownloadTask", e.getMessage()); return "下载失败"; } return "下载成功"; } @Override protected void onProgressUpdate(Integer... progress) { super.onProgressUpdate(progress); progressDialog.setProgress(progress[0]); } @Override protected void onPostExecute(String result) { super.onPostExecute(result); progressDialog.dismiss(); Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show(); } } ``` 最后,在需要下载文件的地方调用AsyncTask执行下载任务: ```java String fileUrl = "http://example.com/file.jpg"; new DownloadTask().execute(fileUrl); ``` 以上代码实现了一个可以下载文件的简单Android应用。它使用了一个异步任务类来执行网络请求并将文件保存到设备的指定路径中。显示了一个进度对话框来显示下载进度,并在下载完成后显示一个Toast消息。请注意,这个示例仅供参考,实际应用可能需要进一步处理异常、权限检查等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值