一、场景
App模拟下载实现自动更新进度条,减少用户等待的焦虑感。
二、实现
安卓实现一个假的文件下载进度条,文件大小随机生成。假的文件下载进度条可以做成一个自定义控件,在进度加载对话框中显示,App开始进行网络请求弹出进度加载对话框,网络请求完成自动关闭对话框。
package com.xp.pro.androiddownlod;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class ProgressBarActivity extends Activity {
private ProgressBar progressBar;
private Button button;
private TextView tvPercent;
private TextView tvTotal;
private Handler handler;
private int progress = 0;
private double total = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress_bar);
progressBar = findViewById(R.id.pb_test);
button = findViewById(R.id.btn_download);
tvPercent = findViewById(R.id.tv_percent);
tvTotal = findViewById(R.id.tv_total);
//生成1-10以内的随机数
Random ra =new Random();
total = ra.nextInt(10)+1;
tvPercent.setText(progress+"%");
tvTotal.setText("0M/"+total+"M");
handler = new Handler(){
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 1:
if(progress<100)
{
progressBar.setProgress(progress);
tvPercent.setText(progress+"%");
tvTotal.setText((total*progress/100)+"M/"+total+"M");
progress++;
handler.sendEmptyMessageDelayed(1,10);
// 模拟耗时的后台计算
}else
{
tvTotal.setText(total+"M/"+total+"M");
tvPercent.setText(progress+"%");
Toast.makeText(ProgressBarActivity.this,"已下载最新版本",Toast.LENGTH_LONG).show();
}
break;
}
}
};
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
handler.sendEmptyMessage(1);
}
});
thread.start();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_percent"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="10dp" />
<TextView
android:id="@+id/tv_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp" />
</LinearLayout>
<ProgressBar
android:id="@+id/pb_test"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp" />
<Button
android:id="@+id/btn_download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="下载" />
</LinearLayout>
后记:有两年时间没写安卓代码,感觉到有一些生疏了。在实际的工作场景中,项目的复杂度要比写个Demo大得多,要考虑代码的耦合和健壮性等等。现在在做业务的时候,偶尔会有一种想要上手撸码的冲动,但我会提醒自己时间精力有限,如果陷入太多的细节,就没有时间去思考业务的提升,无法拓宽视野、修炼全局思维,所以懂技术的产品人需要在理想和现实之前寻求一个合适的平衡点,然后让现实不断逼近理想。
版权声明:本文为u010963246原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。