如何在android中动态启动和结束进度条

发布于 2024-11-10 05:21:18 字数 95 浏览 2 评论 0原文

当我从第一个活动类跳过第二个活动类时,我将开始对第二个活动中的某些图像进行图像处理,然后直到新图像出现在屏幕上时,我不想启动进度栏,然后在新图像出现在屏幕上时完成。我该怎么做?

When I skip second activity class from first activity class, I will start imageprocessing on certain image in second activity and then until new image comes to screen I wnt to start progress bar and then finish when the new image comes to screen. How can I do this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦里人 2024-11-17 05:21:18

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

查看示例代码。
要启动 AsyncTsk,请在要进行图像处理的 Activity 中使用 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;
             }
          }


    }

}

看看这里

Use ProgreaaDialog and AsyncTask. you wil get your soultion
Use AsyncTask in doBackInGroundProcess do image processing. and in doPostExecute() exit or cancel the progress dialog

have a look on the sample code.
To start AsyncTsk use new ProgressTask().execute(null); from the activity where you want to do image processing.

    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;
             }
          }


    }

}

Have a look here

椵侞 2024-11-17 05:21:18

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

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();
}

Try using Async task as shown below:

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();
}
回忆追雨的时光 2024-11-17 05:21:18

这是一个方法,当调用时会启动一个进度条

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:

{
休息;
}
}
进度对话框.dismiss();
}catch(异常e)
{

        }
        }

};

Here is a method which when called starts a progressbar

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)
{

}

}

and here is the handler code

private Handler messageHandler = new Handler() {


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

        case 1:

{
break;
}
}
progressDialog.dismiss();
}catch(Exception e)
{

        }
        }

};
待"谢繁草 2024-11-17 05:21:18

试试这个方法
首先初始化您的 ProgressDialog

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

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

Try this way
first Intialize your ProgressDialog

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

then start a new thread in which you can write your code which needs to be executed
and finally in the handler handle the code and end the progessDialog

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文