dcsimg
August 15, 2018
Hot Topics:

Handling Lengthy Operations in Google's Android, Page 2

  • July 30, 2008
  • By Chunyen Liu
  • Send Email »
  • More Articles »

Performing Lengthy Operations in Child Threads

You implement the child thread in the setRandomPointsInChildThread function. Basically, a new thread will be created and executed. Within the child thread, a function to emulate the lengthy operations is provided in setRandomPoints, which does nothing but generate a series of random points and delay the process by pausing 50 milliseconds between points. After all the points have been prepared, it will send a message within a unique ID, GUIUPDATEIDENTIFIER, defined by you. A deterministic progress dialog is initiated. The screen shot is illustrated in Figure 2.

void setRandomPoints(boolean modeThreaded) {
   int delay_in_msecs = 50;
   int npoints = 120;
   int r = 0, g = 0, b = 0;
   Random rand = new Random();

   if (!modeThreaded)
      mypd = ProgressDialog.show(TutorialOnANR.this,
         "Lengthy Calculations",
         "Please wait...", 
            false);

   mypoint = null;
   mypoint = new MyPoint[npoints];
   for (int i = 0; i < npoints; i++) {
      mypoint[i] = new MyPoint();
      mypoint[i].x = rand.nextFloat() * 320;
      mypoint[i].y = rand.nextFloat() * 240;
      r = (int)(rand.nextFloat() * 155) + 100;
      g = (int)(rand.nextFloat() * 155) + 100;
      b = (int)(rand.nextFloat() * 155) + 100;
      mypoint[i].c = 0xff000000 | (r << 16) | (g << 8) | b;
      mypoint[i].sz = (int)(rand.nextFloat() * 10) + 3

      // Update the progress dialog
      mypd.setProgress(10000 * i / npoints);

      // Emulate the lengthy operations with an intentional
      // delay here
      try {
        Thread.sleep(delay_in_msecs);
      } catch (InterruptedException e) {
      }

   }

   // Dismiss the progress dialog
   mypd.dismiss();
}

// Do lengthy operations in a child thread
private void setRandomPointsInChildThread() {

   // Start a progress dialog here
   mypd = ProgressDialog.show(TutorialOnANR.this,
      "Lengthy Calculations",
      "Please wait...",
      false);

   Thread t = new Thread() {

      public void run() {

         setRandomPoints(true);

         // Send a message to the handler
         Message message = new Message();
         message.what = TutorialOnANR.GUIUPDATEIDENTIFIER;
         TutorialOnANR.this.myGUIUpdateHandler.
            sendMessage(message);
         }
      };

   t.start();
}

Figure 2: Deterministic progress dialog





Page 2 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.

Sitemap

Thanks for your registration, follow us on our social networks to keep up-to-date