android开发步步为营之25:开发自定义进度条对话框

本文介绍如何使用自定义进度对话框实现文件下载功能,包括设计界面、下载进度更新、以及完成下载后的安装流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class ProgressDialog extends AlertDialog
java.lang.Object
   ↳ android.app.Dialog
     ↳ android.app.AlertDialog
       ↳ android.app.ProgressDialog
从类的继承关系我们可以看到ProgressDialog继承至AlertDialog,所以我们今天自己写一个继承至AlertDialog的自定义进度条对话框。完成的功能是:下载盛付通的sdk,下载的过程中显示进度条,下载完毕之后将apk安装用户手机。开始我们的实验。
 
第一步、设计界面
主界面custompdview.xml 放置了一个按钮,用来触发下载apk事件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <Button android:layout_width="wrap_content" android:id="@+id/btncustom" android:text="演示自定义进度对话框" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="50dp"></Button> 
</RelativeLayout>
自定义ProgressDialog界面downloadprogressdialog.xml
放置了一个SeekBar其实和ProgressBar功能一样,就是长得不一样,两个TextView
SeekBar用来显示下载进度,其中一个TextView用来显示当前下载的总量,另外一个用来显示下载的百分比。
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android
    android:layout_gravity="center_vertical" android:layout_width="fill_parent" 
    android:layout_height="wrap_content">
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tvprogressnum" android:text="" android:layout_below="@+id/seekbar" android:layout_alignParentLeft="true" android:layout_marginLeft="84dp"></TextView>
    <SeekBar android:layout_width="match_parent" android:id="@+id/seekbar" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true"></SeekBar>
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tvprogresspercent" android:text="" android:layout_alignTop="@+id/tvprogressnum" android:layout_toRightOf="@+id/tvprogressnum" android:layout_marginLeft="29dp"></TextView> 
</RelativeLayout>
 
第二步、设计下载进度对话框DownloadProgressDialog.java
 
/**
 *
 */
package com.figo.helloworld;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
 
/**
 * 自定义下载文件进度条对话框
 * @author zhuzhifei
 *
 */
public class DownloadProgressDialog extends AlertDialog {
    private SeekBar mProgress;//显示进度
    private TextView mProgressNum;//显示下载总量
    private TextView mProgressPercent;//显示下载百分比
    public static final int M = 1024 * 1024;//M
    public static final int K = 1024;//K
    private double dMax;//文件大小
    private double dProgress;//进度
    private int middle = K;//默认为K
    private int prev = 0;//开始值
    private Handler mViewUpdateHandler;//Handler用来通知更新进度
    private static final NumberFormat nf = NumberFormat.getPercentInstance();//进度格式
    private static final DecimalFormat df = new DecimalFormat("###.##");//文件大小格式
 
    public DownloadProgressDialog(Context context) {
       super(context);
    }
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       LayoutInflater inflater = LayoutInflater.from(getContext());
       mViewUpdateHandler = new Handler() {
           @Override
           public void handleMessage(Message msg) {
              //通过handler更新进度
              super.handleMessage(msg);
              double percent = dProgress / dMax;
              if (prev != (int) (percent * 100)) {
                  mProgress.setProgress((int) (percent * 100));
                  mProgressNum.setText(df.format(dProgress) + "/"
                         + df.format(dMax) + (middle == K ? "K" : "M"));
                  mProgressPercent.setText(nf.format(percent));
                  prev = (int) (percent * 100);
              }
              if (mProgress.getProgress() == 100) {
                  mProgressPercent.setText(nf.format(1));
              }
 
           }
 
       };
        //从自定义的对话框downloadprogressdialog.xml加载页面
       View view = inflater.inflate(R.layout.downloadprogressdialog, null);
       mProgress = (SeekBar) view.findViewById(R.id.seekbar);
       mProgressNum = (TextView) view.findViewById(R.id.tvprogressnum);
       mProgressPercent = (TextView) view.findViewById(R.id.tvprogresspercent);
       setView(view);
       onProgressChanged();
       super.onCreate(savedInstanceState);
 
    }
 
    private void onProgressChanged() {
       mViewUpdateHandler.sendEmptyMessage(0);//发送一条消息触发handleMessage事件
 
    }
    //获取下载文件的最大值
    public double getDMax() {
       return dMax;
    }
    //设置下载文件的最大值
    public void setDMax(double max) {
       if (max > M) {
           middle = M;
       } else {
           middle = K;
       }
       dMax = max / middle;
    }
    //获取当前进度
    public double getDProgress() {
       return dProgress;
    }
    //设置当前进度
    public void setDProgress(double progress) {
       dProgress = progress / middle;
       onProgressChanged();
 
    }
 
}
 
 
第三步、设计Activity CustomPdActivity.java
 /**
 *
 */
package com.figo.helloworld;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
 
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
 
/**
 * @author zhuzhifei
 *
 */
public class CustomPdActivity extends Activity implements OnClickListener {
 
    protected Context context;//上下文
    private DownloadProgressDialog progressDialog;//自定义进度对话框
    private Button btncustom;//按钮
    //加载主界面
    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.custompdview);
       btncustom = (Button) findViewById(R.id.btncustom);
       btncustom.setOnClickListener(this);
       context = getApplicationContext();
       btncustom.setOnClickListener(this);
 
    }
 
    // 写一个下载异步任务
    class DownLoadTask extends AsyncTask<Void, Integer, Uri> {
 
       // 执行任务
       @Override
       protected Uri doInBackground(Void... params) {
           return downLoad();
       }
 
       // 更新进度
       @Override
       protected void onProgressUpdate(Integer... progress) {
           progressDialog.setDProgress(progress[0]);
       }
 
       // 任务执行完成之后
       @Override
       protected void onPostExecute(Uri result) {
           if (progressDialog != null) {
              progressDialog.dismiss();
           }
           if (result == null) {
              Toast.makeText(context, "下载失败,请点击更新按钮重新下载", Toast.LENGTH_SHORT)
                     .show();
              return;
           }
           Toast.makeText(context, "下载完毕,开始安装...", Toast.LENGTH_SHORT).show();
           Intent intent = new Intent(Intent.ACTION_VIEW);
           intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
           intent.setDataAndType(result,
                  "application/vnd.android.package-archive");
           startActivity(intent);
           finish();
       }
 
       // 下载程序
       private Uri downLoad() {
           URL url = null;
           File file = null;
           try {
              // 下载盛付通手机版
 
              url = new URL(
                     "http://spm.shengpay.com/apk/ShengPay-release.apk");
              HttpURLConnection con = (HttpURLConnection) url
                     .openConnection();
              int fileLength = con.getContentLength();
              progressDialog.setDMax(fileLength);// 设置文件大小
              InputStream in = con.getInputStream();
              file = new File(context.getCacheDir(), "ShengPay-release.apk");
              if (!file.exists()) {
                  file.createNewFile();
              }
              // 以下是设置权限,不然没有权限去解压会报没权限:
              // Unable to open zip
              // '/data/data/com.figo.helloworld/cache/ShengPay-release.apk':
              // Permission denied
              Runtime.getRuntime()
                     .exec("chmod 777 " + file.getAbsolutePath());
              FileOutputStream out = new FileOutputStream(file);
              byte[] bytes = new byte[1024];
              int c;
              int count = 0;
              while ((c = in.read(bytes)) != -1) {
                  out.write(bytes, 0, c);
                  if (c < 1024) {
                     publishProgress(count * 1024 + c);
                  } else {
                     count++;
                     publishProgress(count * 1024);
                  }
              }
              in.close();
              out.close();
           } catch (Exception e) {
              e.printStackTrace();
           }
           return Uri.fromFile(file);
           // return flag;
 
       }
    }
 
    // 按钮点击事件
    @Override
    public void onClick(View v) {
       switch (v.getId()) {
       case R.id.btncustom:
           progressDialog = new DownloadProgressDialog(this);
           progressDialog.setTitle("文件下载");
           progressDialog.show();
           // 执行任务
           new DownLoadTask().execute();
           break;
 
       }
    }
 
}
 
第四步、AndroidManifest.xml注册Activity
 
    <activity android:name=".CustomPdActivity">
           <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
 
第五步、运行效果
 
 

资源下载链接为: https://pan.quark.cn/s/22ca96b7bd39 在当今的软件开发领域,自动化构建与发布是提升开发效率和项目质量的关键环节。Jenkins Pipeline作为一种强大的自动化工具,能够有效助力Java项目的快速构建、测试及部署。本文将详细介绍如何利用Jenkins Pipeline实现Java项目的自动化构建与发布。 Jenkins Pipeline简介 Jenkins Pipeline是运行在Jenkins上的一套工作流框架,它将原本分散在单个或多个节点上独立运行的任务串联起来,实现复杂流程的编排与可视化。它是Jenkins 2.X的核心特性之一,推动了Jenkins从持续集成(CI)向持续交付(CD)及DevOps的转变。 创建Pipeline项目 要使用Jenkins Pipeline自动化构建发布Java项目,首先需要创建Pipeline项目。具体步骤如下: 登录Jenkins,点击“新建项”,选择“Pipeline”。 输入项目名称和描述,点击“确定”。 在Pipeline脚本中定义项目字典、发版脚本和预发布脚本。 编写Pipeline脚本 Pipeline脚本是Jenkins Pipeline的核心,用于定义自动化构建和发布的流程。以下是一个简单的Pipeline脚本示例: 在上述脚本中,定义了四个阶段:Checkout、Build、Push package和Deploy/Rollback。每个阶段都可以根据实际需求进行配置和调整。 通过Jenkins Pipeline自动化构建发布Java项目,可以显著提升开发效率和项目质量。借助Pipeline,我们能够轻松实现自动化构建、测试和部署,从而提高项目的整体质量和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值