Android安装过程对话框更新Demo

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

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

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

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

具体实现请看代码:

 

package cn.tch.cdg;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class CustomDialogActivity extends Activity {
	
	private static final int PROGRESS_DIALOG = 0;    
	private Button btnShowDialog;    
	private ProgressThread mProgressThread;    
	private Dialog mDialog;    

    public void onCreate(Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.main);     
        btnShowDialog = (Button) findViewById(R.id.progressDialog);    
        btnShowDialog.setOnClickListener(new OnClickListener(){    
            public void onClick(View v) {    
                showDialog(PROGRESS_DIALOG); 
            }    
        });    
    }    
    protected Dialog onCreateDialog(int id) {    
        switch(id) {    
        case PROGRESS_DIALOG:    
        	mDialog = new AlertDialog.Builder(CustomDialogActivity.this).create();
        	mDialog.setTitle("请稍候");
        	((AlertDialog) mDialog).setMessage("");
        	mProgressThread = new ProgressThread(handler);    
        	mProgressThread.start();    
            return mDialog;    
        default:    
            return null;    
        }    
    }     
    final Handler handler = new Handler() {    
        public void handleMessage(Message msg) {    
            int total = msg.getData().getInt("total");  
            String message = msg.getData().getString("message");
            ((AlertDialog) mDialog).setMessage(message);
            if (total >= 100){    
                dismissDialog(PROGRESS_DIALOG);    
                mProgressThread.setState(ProgressThread.STATE_DONE);    
            }    
        }    
    };     

    private class ProgressThread extends Thread {    
        Handler mHandler;    
        final static int STATE_DONE = 0;    
        final static int STATE_RUNNING = 1;    
        int mState;    
        int mTotal;    
        ProgressThread(Handler handler) {    
            mHandler = handler;    
        }    
        public void run() {    
            mState = STATE_RUNNING;       
            mTotal = 0;    
            while (mState == STATE_RUNNING) {    
                Message msg = mHandler.obtainMessage(); 
                Bundle bundle = new Bundle();    
                bundle.putInt("total", mTotal); 
                bundle.putString("message", "正在安装:"+mTotal); // 设置Message
                msg.setData(bundle);    
                mHandler.sendMessage(msg);    
                mTotal++;    
                
                try {    
                    Thread.sleep(300);    
                } catch (InterruptedException e) {    
                	e.printStackTrace();
                }  
            }    
        }    
        public void setState(int state) {    
            mState = state;    
        }    
    }    
}
<?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"
    >
	<Button
		android:id="@+id/progressDialog"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="显示对话框"/>
</LinearLayout>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值