后台程序运行进度提示(ProgressBar与Handle的整合应用)

后台程序运行进度提示(ProgressBar与Handle的整合应用)

新建一个继承Activity类的ProgressBarAndHandleActivity,并设置布局文件为:progressbarandhandle.xml。

在布局文件中创建一个ProgressBar/TextView/Button

<ProgressBar

        android:id="@+id/progressbarandhandle_pb"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:textSize="22sp" />

 

    <TextView

        android:id="@+id/progressbarandhandle_tv"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/show_information"

        android:textSize="22sp" />

 

    <Button

        android:id="@+id/progressbarandhandle_btn"

        style="@android:style/Widget.Button.Inset"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:layout_marginTop="20dp"

        android:text="@string/button"

        android:textSize="22sp" />

而后在代码中实现各个功能:

package lyx.feng.second;

......

public class ProgressBarAndHandleActivity extends Activity {

    private ProgressBar pb = null;

    private TextView tv = null;

    private Button btn = null;

    // 用于判断结束标记

    private static final int FLAG = 1;

    // 用于判断还没有结束标记

    private static final int INFORMATION = 2;

    // 定义的Handle对象

    private Handler mHandler = new Handler() {

       @SuppressWarnings("static-access")

       public void handleMessage(android.os.Message msg) {

           switch (msg.what) {

           case FLAG:

              // 线程处理完

              tv.setText("100%");

              pb.setProgress(100);

              pb.setVisibility(View.GONE);

              Thread.currentThread().interrupt();

              break;

           case INFORMATION:

              // 线程还没有处理完

              if (!Thread.currentThread().interrupted()) {

                  pb.setProgress((Integer) msg.obj);

                  tv.setText(msg.obj + "%");

              }

              break;

           }

       };

    };

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       super.setContentView(R.layout.progressbarandhandle);

       // 得到实例化对象

       this.pb = (ProgressBar) super

              .findViewById(R.id.progressbarandhandle_pb);

       this.tv = (TextView) super.findViewById(R.id.progressbarandhandle_tv);

       this.btn = (Button) super.findViewById(R.id.progressbarandhandle_btn);

       // 默认是指ProgressBar为不显示

       this.pb.setVisibility(View.GONE);

       // 设置ProgressBar为不显示进度状态

       this.pb.setIndeterminate(false);

       // Button的单机事件

       this.btn.setOnClickListener(new OnClickListener() {

 

           @Override

           public void onClick(View v) {

              // 初始化ProgressBarTextView的一些属性

              pb.setVisibility(View.VISIBLE);

              pb.setMax(100);

              pb.setProgress(0);

              tv.setText(pb.getProgress() + "");

 

              // 定义一个后台线程,用于处理ProgressBar操作

              new Thread() {

 

                  public void run() {

                     // 这里循环100

                     for (int i = 0; i < 100; i++) {

                         try {

                            Thread.sleep(100);

                         } catch (InterruptedException e) {

                            // TODO Auto-generated catch block

                            e.printStackTrace();

                         }

                         if (i == 99) {

                            // 循环结束,传递结束信息给Handle

                            Message msg = new Message();

                            msg.what = FLAG;

                            mHandler.sendMessage(msg);

                         } else {

                            // 循环还没有结束,继续传递普通信息给Handle

                            Message msg = new Message();

                            msg.what = INFORMATION;

                            msg.obj = i;

                            mHandler.sendMessage(msg);

                         }

                     }

                  };

              }.start();

           }

       });

    }

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

阅读更多
想对作者说点什么? 我来说一句

WPF实现ProgressBar气泡显示进度

2014年01月03日 340KB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭