Android 进度对话框从类文件中设置消息

标签 android message progressdialog

我有以下使用 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/

相关文章:

android - Android 中的全局消息池是什么?它驻留在内存中的什么位置?

java - SeekBar - 无法设置起点

java - Android 应用程序中共享首选项声明中出现 NullPointerException?

android - 网页内容消失

django - 如何使用 django-postman 为每个用户提供发送消息的能力

ruby-on-rails - 如何发送通知用户收到私有(private)消息

android - 为什么使用 Retrofit 从服务器到本地数据库同步大量数据时 Android Circle Progress 对话框会卡住

android - 如何使 ProgressDialog 可以通过后退按钮而不是通过屏幕点击取消?

java - 如何正确调用从广播接收器更改 UI 的方法?

android - 可以重获焦点的EditText