Advertisement
fedexist

progressdialog with asynctask

Mar 6th, 2015
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. //Al click del pulsante
  2. public void onClick1(View view){
  3.  
  4.         Log.d("tag","onClick1 è stato aperto");
  5.  
  6.         new ProgressDialogTask().execute();
  7.  
  8.  }
  9.  
  10. //ProgressDialogTask
  11.  
  12. private class ProgressDialogTask extends AsyncTask<Void, Void, Void>{
  13.  
  14.         private ProgressDialog dialog;
  15.  
  16.         ProgressDialogTask(){
  17.  
  18.             dialog = new ProgressDialog(getBaseContext());
  19.  
  20.  
  21.         }
  22.  
  23.         protected void onPreExecute() {
  24.             dialog.setMax(100);
  25.             dialog.setProgress(0);
  26.             dialog.setTitle("Doing Something");
  27.             dialog.setMessage("Please wait!");
  28.             dialog.setCancelable(false);
  29.             dialog.setIndeterminate(true);
  30.  
  31.             dialog.show();
  32.         }
  33.  
  34.         @Override
  35.         protected Void doInBackground(Void... params) {
  36.  
  37.             while(dialog.getProgress() <= dialog.getMax()){
  38.  
  39.                 try{
  40.                     Thread.sleep(5000);
  41.                 }catch(Exception e){
  42.  
  43.                     e.printStackTrace();
  44.                 }
  45.  
  46.                 dialog.incrementProgressBy(5);
  47.  
  48.             }
  49.  
  50.             return null;
  51.         }
  52.  
  53.         @Override
  54.         protected void onPostExecute(Void v){
  55.  
  56.             dialog.dismiss();
  57.         }
  58.  
  59.  
  60.  
  61.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement