package com.commonsware.android.threads;

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.widget.ProgressBar;

public class HandlerDemo extends Activity {

 ProgressBar bar;

 Handler handler = new Handler() {

  @Override

  public void handleMessage(Message msg) {

   bar.incrementProgressBy(5);

  }

 };

 boolean isRunning = false;

 @Override

 public void onCreate(Bundle icicle) {

  super.onCreate(icicle);

  setContentView(R.layout.main);

  bar = (ProgressBar)findViewById(R.id.progress);

 }

 public void onStart() {

  super.onStart();

  bar.setProgress(0);

  Thread background = new Thread(new Runnable() {

   public void run() {

    try {

     for (int i=0; i<20 && isRunning; i++) {

      Thread.sleep(1000);

      handler.sendMessage(handler.obtainMessage ());

     }

    } catch (Throwable t) {

     // just end the background thread

    }

   }

  });

  isRunning = true;

  background.start();

 }

 public void onStop() {

  super.onStop();

  isRunning = false;

 }

}

As part of constructing the Activity, we create an instance of Handler, with our implementation of handleMessage(). Basically, for any message received, we update the ProgressBar by 5 points, then exit the message handler.

In onStart(), we set up a background thread. In a real system, this thread would do something meaningful. Here, we just sleep one second, post a Message to the Handler, and repeat for a total of 20 passes. This, combined with the 5-point increase in the ProgressBar position, will march the bar clear across the screen, as the default maximum value for ProgressBar is 100. You can adjust that maximum via setMax(), such as setting the maximum to be the number of database rows you are processing, and updating once per row.

Note that we then leave onStart(). This is crucial. The onStart() method is invoked on the activity UI thread, so it can update widgets and anything else that affects the UI, such as the title bar. However, that means we need to get out of onStart(), both to let the Handler get its work done, and also so Android does not think our activity is stuck.

The resulting activity is simply a horizontal progress bar (see Figure 15-1).

Figure 15-1. The HandlerDemo sample application

Runnables

If you would rather not fuss with Message objects, you can also pass Runnable objects to the Handler, which will run those Runnable objects on the activity UI thread. Handler offers a set of post...() methods for passing Runnable objects in for eventual processing.

Running in Place

Just as Handler supports post() and postDelayed() to add Runnable objects to the event queue, you can use those same methods on View. This slightly simplifies your code, in that you can then skip the Handler object. However, you lose a bit of flexibility, and the Handler has been around longer in the Android toolkit and may be more tested.

Where, Oh Where Has My UI Thread Gone?

Sometimes, you may not know if you are currently executing on the UI thread of your application. For example, if you package some of your code in a JAR for others to reuse, you might not know whether your code is being executed on the UI thread or from a background thread.

To help combat this problem, Activity offers runOnUiThread(). This works

Вы читаете Beginning Android
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату