Android安装过程对话框更新Demo


最近在做一个批量安装卸载的管理器,在安装的过程中要显示安装信息,比如说:"正在安装XX1.apk 正在安装XX2.apk“当然这个显示是在对话框上面显示的。怎么做呢?实现是这样的:

1、在Activity中重写onCreateDialog(int id)方法;

2、使用Handler更新对话框的信息;

3、用线程监控安装信息,将信息设置在Message中通过Handler发送。

具体实现请看代码:

  1. package cn.tch.cdg;  
  2.   
  3. import Android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.app.Dialog;  
  6. import android.os.Bundle;  
  7. import android.os.Handler;  
  8. import android.os.Message;  
  9. import android.view.View;  
  10. import android.view.View.OnClickListener;  
  11. import android.widget.Button;  
  12.   
  13. public class CustomDialogActivity extends Activity {  
  14.       
  15.     private static final int PROGRESS_DIALOG = 0;      
  16.     private Button btnShowDialog;      
  17.     private ProgressThread mProgressThread;      
  18.     private Dialog mDialog;      
  19.   
  20.     public void onCreate(Bundle savedInstanceState) {      
  21.         super.onCreate(savedInstanceState);      
  22.         setContentView(R.layout.main);       
  23.         btnShowDialog = (Button) findViewById(R.id.progressDialog);      
  24.         btnShowDialog.setOnClickListener(new OnClickListener(){      
  25.             public void onClick(View v) {      
  26.                 showDialog(PROGRESS_DIALOG);   
  27.             }      
  28.         });      
  29.     }      
  30.     protected Dialog onCreateDialog(int id) {      
  31.         switch(id) {      
  32.         case PROGRESS_DIALOG:      
  33.             mDialog = new AlertDialog.Builder(CustomDialogActivity.this).create();  
  34.             mDialog.setTitle("请稍候");  
  35.             ((AlertDialog) mDialog).setMessage("");  
  36.             mProgressThread = new ProgressThread(handler);      
  37.             mProgressThread.start();      
  38.             return mDialog;      
  39.         default:      
  40.             return null;      
  41.         }      
  42.     }       
  43.     final Handler handler = new Handler() {      
  44.         public void handleMessage(Message msg) {      
  45.             int total = msg.getData().getInt("total");    
  46.             String message = msg.getData().getString("message");  
  47.             ((AlertDialog) mDialog).setMessage(message);  
  48.             if (total >= 100){      
  49.                 dismissDialog(PROGRESS_DIALOG);      
  50.                 mProgressThread.setState(ProgressThread.STATE_DONE);      
  51.             }      
  52.         }      
  53.     };       
  54.   
  55.     private class ProgressThread extends Thread {      
  56.         Handler mHandler;      
  57.         final static int STATE_DONE = 0;      
  58.         final static int STATE_RUNNING = 1;      
  59.         int mState;      
  60.         int mTotal;      
  61.         ProgressThread(Handler handler) {      
  62.             mHandler = handler;      
  63.         }      
  64.         public void run() {      
  65.             mState = STATE_RUNNING;         
  66.             mTotal = 0;      
  67.             while (mState == STATE_RUNNING) {      
  68.                 Message msg = mHandler.obtainMessage();   
  69.                 Bundle bundle = new Bundle();      
  70.                 bundle.putInt("total", mTotal);   
  71.                 bundle.putString("message""正在安装:"+mTotal); // 设置Message   
  72.                 msg.setData(bundle);      
  73.                 mHandler.sendMessage(msg);      
  74.                 mTotal++;      
  75.                   
  76.                 try {      
  77.                     Thread.sleep(300);      
  78.                 } catch (InterruptedException e) {      
  79.                     e.printStackTrace();  
  80.                 }    
  81.             }      
  82.         }      
  83.         public void setState(int state) {      
  84.             mState = state;      
  85.         }      
  86.     }      
  87. }  

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <Button  
  8.         android:id="@+id/progressDialog"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="显示对话框"/>  
  12. </LinearLayout>  

相关内容