Advertisement
kusha45

Progress Bar

Feb 14th, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.52 KB | None | 0 0
  1. //1st Activity Intent
  2.  
  3. int delay = 10;
  4. int maxBarValue = 6000;
  5. showDialog(3);
  6. in = new Intent(Home.this, CreateMessage.class);
  7. startActivity(in);
  8.  
  9. //Dialog's Code
  10. protected Dialog onCreateDialog(int id) {
  11.         switch (id) {
  12.         case 3:
  13.             progDialog = new ProgressDialog(Home.this);
  14.             progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  15.             progDialog.setMessage("Loading...");
  16.             progThread = new ProgressThread(handler);
  17.             progThread.start();
  18.             return progDialog;
  19.         }
  20.         return null;
  21.     }
  22.  
  23. final Handler handler = new Handler() {
  24.         public void handleMessage(Message msg) {
  25.             int total = msg.getData().getInt("total");
  26.             progDialog.setProgress(total);
  27.             if (total <= 0) {
  28.                 //dismissDialog(typeBar);
  29.                 progDialog.cancel();
  30.                 progThread.setState(ProgressThread.DONE);
  31.             }
  32.         }
  33.     };
  34.    
  35.     private class ProgressThread extends Thread {
  36.  
  37.         final static int DONE = 0;
  38.         final static int RUNNING = 1;
  39.  
  40.         Handler mHandler;
  41.         int mState;
  42.         int total;
  43.        
  44.         ProgressThread(Handler h) {
  45.             mHandler = h;
  46.         }
  47.  
  48.         @Override
  49.         public void run() {
  50.             mState = RUNNING;
  51.             total = maxBarValue;
  52.             while (mState == RUNNING) {
  53.                 try {
  54.                     Thread.sleep(delay);
  55.                 } catch (InterruptedException e) {
  56.                     Log.e("ERROR", "Thread was Interrupted");
  57.                 }
  58.  
  59.                 Message msg = mHandler.obtainMessage();
  60.                 Bundle b = new Bundle();
  61.                 b.putInt("total", total);
  62.                 msg.setData(b);
  63.                 mHandler.sendMessage(msg);
  64.  
  65.                 total--;
  66.             }
  67.         }
  68.  
  69.         public void setState(int state) {
  70.             mState = state;
  71.         }
  72.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement