简体   繁体   English

关于Android Progress Bar的工作

[英]Regarding Android Progress Bar working

I have a doubt regarding progress bar in android. 我对android中的进度栏​​有疑问。 I haven't implemented it yet but due to shortage of time i want to make sure when i implement i better be clear with what is going to happen and why.. 我还没有实现它,但是由于时间不足,我想确保何时实现,最好弄清楚发生了什么以及为什么。

dialog = new ProgressDialog(this);
        dialog.setCancelable(true);
        dialog.setMessage("Loading...");
        // set the progress to be horizontal
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        // reset the bar to the default value of 0
        dialog.setProgress(0);

        // get the maximum value
        EditText max = (EditText) findViewById(R.id.maximum);
        // convert the text value to a integer
        int maximum = Integer.parseInt(max.getText().toString());
        // set the maximum value
        dialog.setMax(maximum);
        // display the progressbar
        dialog.show();

        // create a thread for updating the progress bar
        Thread background = new Thread (new Runnable() {
           public void run() {
               try {
                   // enter the code to be run while displaying the progressbar.
                   //
                   // This example is just going to increment the progress bar:
                   // So keep running until the progress value reaches maximum value
                   while (dialog.getProgress()<= dialog.getMax()) {
                       // wait 500ms between each update
                       Thread.sleep(500);

                       // active the update handler
                       progressHandler.sendMessage(progressHandler.obtainMessage());
                   }
               } catch (java.lang.InterruptedException e) {
                   // if something fails do something smart
               }
           }
        });

        // start the background thread
        background.start();

    }

    // handler for the background updating
    Handler progressHandler = new Handler() {
        public void handleMessage(Message msg) {
            dialog.incrementProgressBy(increment);
        }
    };

This is the above code i took from somewhere...As this code says i have to keep my actual code below try in order to keep progress bar running.. My doubt is my code is a very lengthy one consisting of nested for loops ( parsing an entire file with all those bits and bytes) .. if i keep my lengthy process at that part of the section, how can it update the progress bar until it finishes its task ? 这是我从某处获取的上述代码...如该代码所述,为了使进度条运行,我必须将实际代码保持在try下面。.我的疑问是我的代码是由嵌套for循环组成的非常长的代码(解析具有所有这些位和字节的整个文件)..如果我在本节的那部分继续进行冗长的过程,它如何更新进度条,直到完成其任务? Am i missing some concept ? 我想念一些概念吗? please explain me how can i keep my process running and also update progress bar? 请向我解释我如何保持我的流程运行并更新进度栏?

You should use AsynTask and show your ProgreesBar in its onPreExecute() method and dismiss the ProgressBar in its onPostExecute() method. 您应该使用AsynTask并显示在您的ProgreesBar onPreExecute()方法,并驳回其进度onPostExecute()方法。 Do all your Loading Stuff in doInBackground() method.Further, to update ProgressBar use onProgressUpdate() doInBackground()方法中doInBackground()所有加载工作。 doInBackground() ,要使用onProgressUpdate()更新ProgressBar。

This will help : http://developer.android.com/reference/android/os/AsyncTask.html 这将有所帮助: http : //developer.android.com/reference/android/os/AsyncTask.html

I think the concept that you are missing is that if you want to to multiple things (showing progress in a progress bar and doing some actual work) you need to use multiple threads. 我认为您所缺少的概念是,如果您想处理多种事情(在进度栏中显示进度并进行一些实际工作),则需要使用多个线程。 That is why you need to create a separate thread to do the actual work. 这就是为什么您需要创建一个单独的线程来进行实际工作的原因。

And even if your code is really length, try re-factoring it, splitting it up into new classes and methods. 即使您的代码确实很长,也请尝试对其进行重构,将其拆分为新的类和方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM