Example ProgressDialog with a second thread

Android 程序开发一定会设计网络数据下载,不免出现等待时间,为了缓和用户的等待时间的“烦躁”,一般会使用进度对话框提升用户正在进行数据下载,请稍候云云~
下面让我们看一下android sdk的事例代码是怎么指导我们完成这样的工作的。

1、布局文件:
<? xml version ="1.0" encoding ="utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
   android:orientation ="vertical" android:layout_width ="fill_parent"
   android:layout_height ="fill_parent" >
   < TextView android:layout_width ="fill_parent"
     android:layout_height ="wrap_content" android:text ="@string/hello" />
   < Button android:id ="@+id/progressBtn" android:layout_width ="wrap_content"
     android:layout_height ="wrap_content" android:text ="HelloProgressBar" />
</ LinearLayout >

2、Java代码:
package com.penguin7.hello;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;

public class HelloAndroid extends Activity {
   private static final int PROGRESS_DIALOG = 0;
   private ProgressDialog mProgressDlg = null;
   private ProgressThread mProgressThd = null;

  @Override
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
     // Setup the button that starts the progress dialog
    Button btnProgress = (Button) findViewById(R.id.progressBtn);
    btnProgress.setOnClickListener( new View.OnClickListener() {
       public void onClick(View v) {
        showDialog(PROGRESS_DIALOG);
      }
    });
  }

  @Override
   protected Dialog onCreateDialog( int id) {

     switch (id) {
     case PROGRESS_DIALOG:
      mProgressDlg = new ProgressDialog( this);
      mProgressDlg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
      mProgressDlg.setTitle( "Loading...");
      mProgressDlg.setMessage( "Please wait for a while...");
                        mProgressThd = new ProgressThread(handler);
                        mProgressThd.start();
       return mProgressDlg;
     default:
       return null;
    }
  }

   // Define the Handler that receives messages from the thread and update the
   // progress
   private final Handler handler = new Handler() {
     public void handleMessage(Message msg) {
       int total = msg.getData().getInt( "total");
      mProgressDlg.setProgress(total);
       if (total >= 100) {
        dismissDialog(PROGRESS_DIALOG);
        mProgressThd.setState(ProgressThread.STATE_DONE);
      }
    }
  };

   /** Nested class that performs progress calculations (counting) */
   private class ProgressThread extends Thread {
    Handler mHandler = null;
     private final static int STATE_DONE = 0;
     private final static int STATE_RUNNING = 1;
     private int mState = 0;

    ProgressThread(Handler h) {
       this.mHandler = h;
    }

     public void run() {
      setState(STATE_RUNNING);
       int total = 0;
       while (STATE_RUNNING == mState) {
         try {
          Thread.sleep(100);
        } catch (InterruptedException e) {
          Log.e( "ERROR", "Thread Interrupted");
        }
        Message msg = mHandler.obtainMessage();
        Bundle b = new Bundle();
        b.putInt( "total", total);
        msg.setData(b);
        mHandler.sendMessage(msg);
        total++;
      }
    }

     /**
     * sets the current state for the thread, used to stop the thread
     */
     public void setState( int state) {
      mState = state;
    }
  }

}

3、看看效果:

你可能感兴趣的:(android,职场,休闲,进度对话框,新线程)