Android 进度对话框仅更新 Message ,而不是栏和线条 [英] Android progress Dialog only updating Message , not the bar and the lines

查看:15
本文介绍了Android 进度对话框仅更新 Message ,而不是栏和线条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要实现FTP下载功能.它有效,但在报告进度时,我只发现只有消息发生变化,而数字和栏没有变化

I am going to implement the FTP download function. It works but when it comes to reporting the progress, I have only found that only the message changes but not the numbers and the bar changing

我的屏幕截图如下

以下是我的代码:

public void buttonExit(View v) {
    System.exit(1); 
}

public void doClick(View v){

    //ftp

    filename = null;
    filename = "giant.mp4";

    pd = new ProgressDialog(this);
    pd.setTitle("EOrder");
    pd.setMessage("Downloading file. Please wait...");
    pd.setIndeterminate(true);
    pd.setMax(100);
    pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    pd.setProgress(0);
    pd.setCancelable(false);
    pd.show();

    ad = new AlertDialog.Builder(this);
    ad.setTitle("EOrder");
    ad.setMessage("Download Finish");
    ad.setNeutralButton("OK", null);

    new FTPDownload().execute(filename);
}

class FTPDownload extends AsyncTask<String , String , Void>{

    boolean running = true;

    Date today = Calendar.getInstance().getTime();    
    Format formatter = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
    String reportDate = formatter.format(today);


    String fileAndroid = reportDate + "_" + filename;

    @Override
    protected Void doInBackground(String... params) {
        // TODO Auto-generated method stub
        Log.d("******", "Background thread starting......"); 

        FTPClient client = new FTPClient();

        try {
            Log.d("tag" , "arrived");

            client.connect("newaswrissssing.win5.siteonlinetest.com");
            boolean successLogin = client.login("newaswrissssing", "newaswrissssing2014cap!");


            if(successLogin){
                client.changeWorkingDirectory("dummy");
                Log.d("tag" , "success");
                long fileSize = getFileSize(client , params[0]);
                client.setFileType(FTP.BINARY_FILE_TYPE);
                client.enterLocalPassiveMode();

                File instanceRecordDirectory = new File(Environment.getExternalStorageDirectory() + File.separator + "TVB_Movie");

                if(!instanceRecordDirectory.exists()){
                    instanceRecordDirectory.mkdirs();
                }

                File instanceRecord = new File(instanceRecordDirectory.getAbsolutePath() + File.separator + fileAndroid);

                if(!instanceRecord.exists()){
                    instanceRecord.createNewFile();
                }

                BufferedOutputStream desFileStream = new BufferedOutputStream(new FileOutputStream(instanceRecord.getAbsolutePath()),8*1024);


                InputStream in = client.retrieveFileStream(params[0]);

                Log.d("as" , String.valueOf(in));

                byte[] buffer = new byte[1024];
                int len1 = 0;
                long total = 0;

                while ((len1 = in.read(buffer)) > 0) {
                    total += len1; //total = total + len1
                    Log.d("aDDs" , String.valueOf(in));
                    publishProgress("" + (int)((total*100)/fileSize));
                    desFileStream.write(buffer, 0, len1);
                }

                desFileStream.flush();
                desFileStream.close();

                client.completePendingCommand();
                in.close();

            }else{
                Log.d("tag" , "sosad");
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                client.logout();
                client.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    protected void onProgressUpdate(String... progress) {
        super.onProgressUpdate(progress);           
        Log.d("progress","values: "+progress[0]);
        pd.incrementProgressBy(Integer.parseInt(progress[0]));
        pd.setMessage("Downloading file... Progress at " + progress[0] + "%");      
    }

    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        pd.dismiss();
          ad.show();
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

你能告诉我除了使用 pd.incrementProgressBy(Integer.parseInt(progress[0]));setProgress 之外还有什么步骤吗?

Would you please tell me what other steps to do besides using pd.incrementProgressBy(Integer.parseInt(progress[0])); or setProgress?

推荐答案

您的 ProgressDialog 不应中断.您应该使用 false 而不是 true

your ProgressDialog should not be intederminate. You should call setIndeterminate with false instead of true

pd.setIndeterminate(false);

你应该使用 setProgress 而不是 incrementProgressBy

and you should use setProgress instead of incrementProgressBy

这篇关于Android 进度对话框仅更新 Message ,而不是栏和线条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆