新增功能:进度条显示更新进度

1.把下载和查询下载进度的方法分开

//服务器中APK下载地址
    private String updateurl;
    private Context context;
// 下载应用的进度条
    private ProgressDialog mProgressDialog;
//下载唯一ID
    private long refernece;
    private DownloadManager downloadManager;
    /**
     * 从服务器中下载APK
     */
    private void downLoadApk() {
        if (TextUtils.isEmpty(updateurl)) {
            return;
        }
        try {
            String serviceString = Context.DOWNLOAD_SERVICE;
            context = this.getApplicationContext();
            downloadManager = (DownloadManager) context.getSystemService(serviceString);
            //将下载地址url放入uri中
            Uri uri = Uri.parse(updateurl);
            DownloadManager.Request request = new DownloadManager.Request(uri);
            request.allowScanningByMediaScanner();
            request.setVisibleInDownloadsUi(true);
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setMimeType("application/vnd.android.package-archive");
            //文件如果存在,则删除原来文件
            File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/app-debug/", "app-debug.apk");
            if (file.exists()) {
                file.delete();
            }         request.setDestinationInExternalPublicDir(Environment.getExternalStorageDirectory().getAbsolutePath() + "/app-debug/", "app-debug.apk");
            //获得唯一下载id
            refernece = downloadManager.enqueue(request);
            //将id放进Intent
            Intent localIntent = new Intent(BROADCAST_ACTION);
            localIntent.putExtra(EXTENDED_DATA_STATUS, refernece);
        } catch (Exception exception) {
            Toast.makeText(getApplicationContext(), "下载新版本失败", Toast.LENGTH_SHORT).show();
            loginMain();
        }
    }
/**
     * 查询下载状态
     */
private void searchStatus(){
        String serviceString = Context.DOWNLOAD_SERVICE;
        context = this.getApplicationContext();
        downloadManager = (DownloadManager) context.getSystemService(serviceString);
            //查询下载信息
            DownloadManager.Query query=new DownloadManager.Query();
            query.setFilterById(refernece);
            try{
                boolean isGoging=true;
                while(isGoging){
                    Cursor cursor = downloadManager.query(query);
                    if (cursor != null && cursor.moveToFirst()) {
                        //获取当前下载量
                        long downloadedBytes = cursor.getLong(
                                cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                        long totalBytes = cursor.getLong(
                                cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
                        mProgressDialog.setMax(((int) (totalBytes / 1024)));
                        int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
                        switch(status){
                            case DownloadManager.STATUS_RUNNING:
                                mProgressDialog.setProgress(((int) (downloadedBytes / 1024)));
                                if(!mProgressDialog.isShowing()){
                                    mProgressDialog.show();
                                }
                                break;
                            //如果下载状态为成功
                            case DownloadManager.STATUS_SUCCESSFUL:
                                isGoging=false;
                                mProgressDialog.dismiss();
                                installApkDialog();
                             case DownloadManager.STATUS_FAILED:
                                isGoging = false;
                                Toast.makeText(getApplicationContext(), "下载新版本失败",  Toast.LENGTH_SHORT).show();
                                loginMain();
                                break;
                            case DownloadManager.STATUS_PAUSED:
                                searchReason();
                                break;
                        }
                    }

                    if(cursor!=null){
                        cursor.close();
                    }
                }
            }catch(Exception e){
                e.printStackTrace();
            }
    }

2.显示进度条

/**
     * 下载进度显示
     */
    private void getProgressDialog() {
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setProgressNumberFormat("%1d KB/%2d KB");
        mProgressDialog.setMessage("正在更新...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//水平进度条
        mProgressDialog.setCancelable(true);//设置是否可以通过点击Back键取消
        mProgressDialog.show();
        new Thread(){
            public void run(){
                try {
                    Looper.prepare();
                    downLoadApk();
                    searchStatus();
                    Looper.loop();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }.start();

    }

Android中的Looper类,是用来封装消息循环和消息队列的一个类,用于在android线程中进行消息处理。handler其实可以看做是一个工具类,用来向消息队列中插入消息的。
先调用Looper.prepare()启用Looper;Looper.loop(); loop函数从MessageQueue中从前往后取出Message,Looper.loop()中是个while循环,只有对它所在线程的Looper调用了quit()函数,Looper.loop()函数才能完成。

你可能感兴趣的:(新增功能:进度条显示更新进度)