我有以下使用 AsyncTask 下载视频的代码。
//DOWNLOAD VIDEOS
private class downloadVideosAsync extends AsyncTask <String, String, String>{
protected void onPreExecute(){
super.onPreExecute();
MyActivity.this.mProgressDialog.setMessage("Downloading Videos...");
}
@Override
protected String doInBackground(String... strings){
try{
VideosC.downloadVideos( VideosM.getVideoNames(), VideosM.getVideoUrls(),
VideosM.getVideoThumbs(), VideosM.getFileModified() );
}catch (NullPointerException e){
Log.e(LOG_TAG, e.toString());
}catch(Exception e){
Log.e(LOG_TAG, e.toString());
}
return null;
}
@Override
protected void onPostExecute(String lenghtOfFile) {
new downloadSlideshowsAsync().execute();
}
}
如您所见,我已经为我的进度对话框设置了消息“正在下载视频...”。现在我想做的是类似 setMessage("Downloading 1 of 5") 的事情。但问题是我的 downloadVideos 函数在另一个类文件 VideosController.java 中
public void downloadVideos( ArrayList<String> VideoNames, ArrayList<String> VideoUrls,
ArrayList<String> VideoThumbs, ArrayList<String> fileModified){
try{
int x;
int videoNamesLenght = VideoNames.size();
File vidFile;
for(x = 0; x < videoNamesLenght; x++) {
String[] videoName = VideoNames.get(x).split("/");
String currentFile = videoName[0] + "." + videoName[1];
String currentFileURL = VideoUrls.get(x) + VideoNames.get(x);
Log.v(LOG_TAG, "currentFileURL: " + currentFileURL);
vidFile = new File(Environment.getExternalStorageDirectory()
+ "/MyApp/Downloads/Videos/", currentFile);
//I want to do maybe here something like
//mProgressDialog.setMessage("Downloading x of y")
downloadVideoFile(currentFile, currentFileURL, vidFile, fileModified.get(x));
}
}catch(Exception e){
Log.e(LOG_TAG, e.toString());
}
}
有什么想法吗?非常感谢您的帮助! :)
最佳答案
您可以在 Activity 中定义一个调用 VideosController 的静态函数。 在该函数中,您可以发送处理程序消息并对其进行处理。
关于Android 进度对话框从类文件中设置消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9527157/