Question

I want show a progress dialog (with a progressbar or circle this is indifferent to me) when i click the button. Actually i can show it but i don't know how hide it after finish the operations inside at onclick. This is the code:

copy.setOnClickListener(new OnClickListener() {         
            public void onClick(View v){

                                ProgressDialog progressDialog = new ProgressDialog(getActivity());
                                progressDialog.setProgressStyle(R.style.NewDialog);
                                progressDialog.setMessage("Loading...");
                                progressDialog.show(); 
                                String datafolder = Environment.getDataDirectory().getAbsolutePath()+File.separator+"app";
                                File customfolder=new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString()+File.separator+"BackupApps");
                                String comando = "cp -r /data/app /sdcard/BackupApps";
                                Process suProcess = null;
                                try {
                                    suProcess = Runtime.getRuntime().exec("su");
                                } catch (IOException e3) {
                                    // TODO Auto-generated catch block
                                    e3.printStackTrace();
                                }
                                DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());

                                try {

                                    os.writeBytes(comando + "\n");

                                } catch (IOException e2) {
                                    // TODO Auto-generated catch block
                                    e2.printStackTrace();

                                }
                                try {
                                    os.flush();
                                } catch (IOException e1) {
                                    // TODO Auto-generated catch block
                                    e1.printStackTrace();
                                }
                                try {
                                    os.writeBytes("exit\n");

                                } catch (IOException e1) {
                                    // TODO Auto-generated catch block
                                    e1.printStackTrace();
                                }
                                try {
                                    os.flush();

                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();

                                }   

                            }


        });

How can i do it?

Était-ce utile?

La solution

First define inner class like this in your class

    class SaveTask extends AsyncTask<Void, Void, Void> {

    ProgressDialog progressDialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = new ProgressDialog(getActivity());
        progressDialog.setProgressStyle(R.style.NewDialog);
        progressDialog.setMessage("Loading...");
        progressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        String datafolder = Environment.getDataDirectory()
                .getAbsolutePath() + File.separator + "app";
        File customfolder = new File(Environment
                .getExternalStorageDirectory().getAbsolutePath().toString()
                + File.separator + "BackupApps");
        String comando = "cp -r /data/app /sdcard/BackupApps";
        Process suProcess = null;
        try {
            suProcess = Runtime.getRuntime().exec("su");
        } catch (IOException e3) {
            // TODO Auto-generated catch block
            e3.printStackTrace();
        }
        DataOutputStream os = new DataOutputStream(
                suProcess.getOutputStream());

        try {

            os.writeBytes(comando + "\n");

        } catch (IOException e2) {
            // TODO Auto-generated catch block
            e2.printStackTrace();

        }
        try {
            os.flush();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            os.writeBytes("exit\n");

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            os.flush();

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (progressDialog != null) {
            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        }
    }

}

call this in your onClick() like this.. here we haven't passing any data to AsynchTask so here i am passing null

copy.setOnClickListener(new OnClickListener() {         
            public void onClick(View v){
    SaveTask task = new SaveTask();
    task.execute(null, null, null);
}

Autres conseils

Start a AsyncTask on onClick method like this:

public void onClick(View v){
    YourAsyncTask asyncTask = new AsyncTask(context);
    asyncTask.execute();
}

And in your AsyncTask create a ProgressDialog on onPreExecute(), do your stuff in doInBackground() and dismiss the ProgressDialog on onPostExecute():

public class YourAsyncTask extends AsyncTask<Void, Void, Void> {
private Context ctx;
private ProgressDialog progressDialog;

public YourAsyncTask(Context ctx) {
      this.ctx = ctx;
}

@Override
protected void onPreExecute() {
  ProgressDialog progressDialog = new ProgressDialog(getActivity());
      progressDialog.setProgressStyle(R.style.NewDialog);
      progressDialog.setMessage("Loading...");
      progressDialog.show(); 
}


    @Override
    protected Void doInBackground(Void... params) {
                 String datafolder = Environment.getDataDirectory().getAbsolutePath()+File.separator+"app";
                 File customfolder=new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString()+File.separator+"BackupApps");
                 String comando = "cp -r /data/app /sdcard/BackupApps";
                 Process suProcess = null;
                 try {
                       suProcess = Runtime.getRuntime().exec("su");
                 } catch (IOException e3) {
                 // TODO Auto-generated catch block
                      e3.printStackTrace();
                 }
                 DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());

                            try {

                                os.writeBytes(comando + "\n");

                            } catch (IOException e2) {
                                // TODO Auto-generated catch block
                                e2.printStackTrace();

                            }
                            try {
                                os.flush();
                            } catch (IOException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                            }
                            try {
                                os.writeBytes("exit\n");

                            } catch (IOException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                            }
                            try {
                                os.flush();

                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();

                            }   

                        }
    }
@Override
protected void onPostExecute(Void res) {
      progressDialog.hide();
      progressDialog.dismiss();
}

don't foget to declare the progressDialog global variable

 @Override 
public void onClick(View v) {
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {

    @Override
    protected void onPreExecute() {
                progressDialog = new ProgressDialog(getActivity());
                progressDialog.setProgressStyle(R.style.NewDialog);
                progressDialog.setMessage("Loading...");
                progressDialog.show(); 
    }

    @Override
    protected Void doInBackground(Void... arg0) {
        String datafolder = Environment.getDataDirectory().getAbsolutePath()+File.separator+"app";
                            File customfolder=new File(Environment.getExternalStorageDirectory().getAbsolutePath().toString()+File.separator+"BackupApps");
                            String comando = "cp -r /data/app /sdcard/BackupApps";
                            Process suProcess = null;
                            try {
                                suProcess = Runtime.getRuntime().exec("su");
                            } catch (IOException e3) {
                                // TODO Auto-generated catch block
                                e3.printStackTrace();
                            }
                            DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());

                            try {

                                os.writeBytes(comando + "\n");

                            } catch (IOException e2) {
                                // TODO Auto-generated catch block
                                e2.printStackTrace();

                            }
                            try {
                                os.flush();
                            } catch (IOException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                            }
                            try {
                                os.writeBytes("exit\n");

                            } catch (IOException e1) {
                                // TODO Auto-generated catch block
                                e1.printStackTrace();
                            }
                            try {
                                os.flush();

                            } catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();

                            }   
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        if (progressDialog!=null) {
            progressDialog.dismiss();
        }
    }

};
task.execute((Void[])null);

}

Add progressDialog.dismiss(); in the last line on onClick and in all the catch so that it will disappear even if an Exception is thrown

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top