Android 下载后检查状况
我的活动中有一个类,它下载一个mp3文件,然后返回一些数据。在下载完成之前,它会检查一个条件。下载过程在后台完成 我想在下载完成后执行条件检查。你能说说这样做的方法吗 更新 它工作正常,但在完成下载resultFromDownload=getFromSdcard()之后;执行。 但在此之后,我希望它返回以下语句:Android 下载后检查状况,android,eclipse,Android,Eclipse,我的活动中有一个类,它下载一个mp3文件,然后返回一些数据。在下载完成之前,它会检查一个条件。下载过程在后台完成 我想在下载完成后执行条件检查。你能说说这样做的方法吗 更新 它工作正常,但在完成下载resultFromDownload=getFromSdcard()之后;执行。 但在此之后,我希望它返回以下语句: return resultFromDownload; 但onPostExecute之后什么也没有发生 private String downloadSomeFiles(Stri
return resultFromDownload;
但onPostExecute之后什么也没有发生
private String downloadSomeFiles(String filename) {
DownloadImage downloadimage = new DownloadImage();
downloadimage.execute(filename);
**return resultFromDownload;**
}
此任务:
private class DownloadImage extends AsyncTask<String, Integer, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
// This is run in a background thread
@Override
protected Boolean doInBackground(String... params) {
// get the string from params, which is an array
Boolean resul = true;
try{
DownloadRequest downloadRequestLrc = new DownloadRequest()
.downloadPath("http://192.168.1.108/music+".mp3)
.filepath(G.DIR_APP + "/" + music+ ".mp3")
.simulate(true)
.download();
downloadRequests =downloadRequestLrc;
} catch (Exception e){
resul=false;
}
return resul;
}
// This is called from background thread but runs in UI
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
// Do things like update the progress bar
}
// This runs in UI when background thread finishes
@Override
protected void onPostExecute(Boolean result) {
if(result){
// Log.i("resulyyyy : ",result);
**resultFromDownload= getFromSdcard();**
Log.i("result1 : ",resultFromDownload);
} else {
resultFromDownload="";
Log.i("result2 : ",resultFromDownload);
}
// Do things like hide the progress bar or change a TextView
}
}
私有类下载映像扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
//这是在后台线程中运行的
@凌驾
受保护的布尔doInBackground(字符串…参数){
//从params获取字符串,它是一个数组
布尔结果=真;
试一试{
DownloadRequest downloadRequestLrc=新的DownloadRequest()
.下载路径(“http://192.168.1.108/music+“.mp3)
.filepath(G.DIR_应用程序+“/”+音乐+“.mp3”)
.模拟(真实)
.download();
downloadRequests=downloadRequestLrc;
}捕获(例外e){
结果=假;
}
返回结果;
}
//这是从后台线程调用的,但在UI中运行
@凌驾
受保护的void onProgressUpdate(整型…值){
super.onProgressUpdate(值);
//执行诸如更新进度条之类的操作
}
//当后台线程完成时,它在UI中运行
@凌驾
受保护的void onPostExecute(布尔结果){
如果(结果){
//Log.i(“resultyyyy:,result”);
**resultFromDownload=getFromSdcard()**
Log.i(“result1:”,resultFromDownload);
}否则{
resultFromDownload=“”;
Log.i(“result2:,resultFromDownload”);
}
//执行隐藏进度条或更改文本视图等操作
}
}
您可以创建一个AsyncTask
类来完成上述操作
例如:
private class DownloadImage extends AsyncTask<String, Integer, Boolean> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
// This is run in a background thread
@Override
protected Boolean doInBackground(String... params) {
// get the string from params, which is an array
Boolean resul = true;
try{
DownloadRequest downloadRequestLrc = new DownloadRequest()
.downloadPath("http://192.168.1.108/music.mp3")
.filepath(G.DIR_APP + "/" + params[0] + ".mp3")
.simulate(true)
.download();
downloadRequests =downloadRequestLrc;
}
catch (Exception e){
resul=false;
}
return resul;
}
// This is called from background thread but runs in UI
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
// Do things like update the progress bar
}
// This runs in UI when background thread finishes
@Override
protected void onPostExecute(Boolean result) {
if(result){
//All allright
}
else{
//Problem!
}
// Do things like hide the progress bar or change a TextView
}
}
DownloadImage downloadimage = new DownloadImage();
downloadimage.execute(filename));
您可以将其放入
AsyncTask
中,并在onPostExecute()中完成后执行所需操作。
onPostExecute的结果是什么