How to start and finish progressBar dynamically in android

前端 未结 4 959
刺人心
刺人心 2021-01-07 09:15

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

4条回答
  •  遥遥无期
    2021-01-07 10:07

    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 {
            private ProgressDialog dialog;
            List titles;
            private ListActivity activity;
            //private List 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 titles = new ArrayList(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

提交回复
热议问题