如何在android中动态启动和完成progressBar

时间:2011-05-27 09:57:15

标签: android android-layout android-widget progress-bar

当我从第一个活动类跳过第二个活动类时,我将在第二个活动中开始对某个图像进行图像处理,然后直到新图像进入屏幕我才开始进度条,然后在新图像进入屏幕时结束。我怎么能这样做?

4 个答案:

答案 0 :(得分:2)

使用ProgreaaDialog和AsyncTask。你会得到你的灵魂 在doBackInGroundProcess中使用AsyncTask进行图像处理。并在doPostExecute()中退出或取消进度对话框

查看示例代码。 要启动AsyncTsk,请使用您要进行图像处理的活动中的new ProgressTask().execute(null);

    private class ProgressTask extends AsyncTask<String, Void, Boolean> {
        private ProgressDialog dialog;
        List<Message> titles;
        private ListActivity activity;
        //private List<Message> messages;
        public ProgressTask(ListActivity activity) {
            this.activity = activity;
            context = activity;
            dialog = new ProgressDialog(context);
        }



        /** progress dialog to show user that the backup is processing. */

        /** application context. */
        private Context context;

        protected void onPreExecute() {
            this.dialog.setMessage("Progress start");
            this.dialog.show();
        }

            @Override
        protected void onPostExecute(final Boolean success) {
                List<Message> titles = new ArrayList<Message>(messages.size());
                for (Message msg : messages){
                    titles.add(msg);
                }
                MessageListAdapter adapter = new MessageListAdapter(activity, titles);
                activity.setListAdapter(adapter);
                adapter.notifyDataSetChanged();

                if (dialog.isShowing()) {
                dialog.dismiss();
            }

            if (success) {
                Toast.makeText(context, "OK", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();
            }
        }

        protected Boolean doInBackground(final String... args) {
            try{    
                BaseFeedParser parser = new BaseFeedParser();
                messages = parser.parse();


                return true;
             } catch (Exception e){
                Log.e("tag", "error", e);
                return false;
             }
          }


    }

}

拥有look here

答案 1 :(得分:1)

尝试使用Async任务,如下所示:

try{
class test extends AsyncTask{


     TextView tv_per;
     int mprogress;

    Dialog UpdateDialog = new Dialog(ClassContext);

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        mprogress = 0;

        UpdateDialog.setTitle(getResources().getString(R.string.app_name));
        UpdateDialog.setContentView(R.layout.horizontalprogressdialog);
        TextView dialog_message =  (TextView)UpdateDialog.findViewById(R.id.titleTvLeft);
        tv_per = (TextView)UpdateDialog.findViewById(R.id.hpd_tv_percentage);
        dialog_message.setText(getResources().getString(R.string.dialog_retrieving_data));
        dialog_message.setGravity(Gravity.RIGHT);
        UpdateDialog.setCancelable(false);
        UpdateDialog.show();
        super.onPreExecute();
    }



    @Override
    protected void onProgressUpdate(Object... values) {
        // TODO Auto-generated method stub
        ProgressBar update = (ProgressBar)UpdateDialog.findViewById(R.id.horizontalProgressBar);
        update.setProgress((Integer) values[0]);
        int percent =  (Integer) values[0];
        if(percent>=100)
        {
            percent=100;
        }
        tv_per = (TextView)UpdateDialog.findViewById(R.id.hpd_tv_percentage);
         tv_per.setText(""+percent);
    }



    @Override
    protected Object doInBackground(Object... params) {
        // TODO Auto-generated method stub
        //your code
}

        super.onPostExecute(result);
        UpdateDialog.dismiss();
    }

 }
 new test().execute(null);

 }
catch(Exception e)
{
 e.printStackTrace();
}

答案 2 :(得分:0)

试试这种方式 首先初始化您的ProgressDialog

 progressDialog = ProgressDialog.show(this, "", "Trying to ...");

然后启动一个新线程,您可以在其中编写需要执行的代码  最后在处理程序中处理代码并结束progessDialog

答案 3 :(得分:0)

这是一种方法,在调用时启动进度条

private void downloadText(String urlStr) {

    final String url = urlStr;
     progressDialog = ProgressDialog.show(this, "", "Trying to register...");
    Log.i("First string", urlStr);
try{
    new Thread () {
        public void run() {
            int BUFFER_SIZE = 2000;
            InputStream in = null;
            try{
           msg = Message.obtain();
            msg.what=1;
            }catch(Exception e)
            {
            }

            try {
                in = openHttpConnection(url);
           InputStreamReader isr = new InputStreamReader(in);
                int charRead;
                  text = "";
                  char[] inputBuffer = new char[BUFFER_SIZE];

                      while ((charRead = isr.read(inputBuffer))>0)
                      {                    
                          //---convert the chars to a String---
                          String readString = 
                          String.copyValueOf(inputBuffer, 0, charRead);                    
                          text += readString;
                          inputBuffer = new char[BUFFER_SIZE];
                      }
                     Bundle b = new Bundle();
                        b.putString("text", text);
                        msg.setData(b);
                      in.close();

            }catch (Exception e) {

            //////////////////////////////////////  
                e.printStackTrace();
            }
            try{
            messageHandler.sendMessage(msg);
            }catch(Exception e)
            {
            }
        }
    }.start();
}catch(Exception e)
{

}

}

这是处理程序代码

private Handler messageHandler = new Handler() {


    public void handleMessage(Message msg) {
        try{
        super.handleMessage(msg);
        switch (msg.what) {

        case 1:

{ 打破; }             }             progressDialog.dismiss();             } catch(例外e)             {

        }
        }

};