我有以下使用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(“下载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());
}
}
有什么想法吗?非常感谢您的帮助! :)
答案 0 :(得分:1)
您可以在Activity中定义一个静态函数来调用VideosController。 在该功能中,您可以发送处理程序消息并进行处理。