2013年10月23日,今天是在“我在找你信息服务有限公司”第一天上班,公司给提出了这样一个要求:下载本公司的app,并且在下载的过程中要在状态栏中显示下载的进度,并且,可以暂停和继续下载。
下面是我的代码实现:
MainActivity.java
1 package com.yt.downloader; 2 3 import java.io.File; 4 5 import net.tsz.afinal.FinalHttp; 6 import net.tsz.afinal.http.AjaxCallBack; 7 8 import android.os.Bundle; 9 import android.os.Environment; 10 import android.os.Handler; 11 import android.os.Message; 12 import android.os.Vibrator; 13 import android.annotation.SuppressLint; 14 import android.app.Activity; 15 import android.app.AlertDialog; 16 import android.app.Notification; 17 import android.app.NotificationManager; 18 import android.content.Context; 19 import android.content.DialogInterface; 20 import android.util.Log; 21 import android.view.Menu; 22 import android.view.View; 23 import android.view.View.OnClickListener; 24 import android.widget.Button; 25 import android.widget.EditText; 26 import android.widget.RemoteViews; 27 import android.widget.Toast; 28 29 public class MainActivity extends Activity { 30 31 public static final String TAG = "MainActivity"; 32 33 private EditText downloadUrlEt; 34 private Button downloadBtn; 35 private Button pauseBtn; 36 37 private NotificationManager manager; 38 39 private Notification notification; 40 41 private AjaxCallBackcallBack; 42 43 private RemoteViews contentView; 44 45 private long progress; 46 47 private boolean isPaused; 48 49 private Message msg; 50 51 @SuppressLint("HandlerLeak") 52 private Handler handler = new Handler() { // 更改进度条的进度 53 54 public void handleMessage(Message msg) { 55 56 contentView.setProgressBar(R.id.pb, 100, (int) progress, false); 57 58 notification.contentView = contentView; 59 60 manager.notify(0, notification); 61 62 super.handleMessage(msg); 63 64 }; 65 66 }; 67 68 @Override 69 protected void onCreate(Bundle savedInstanceState) { 70 super.onCreate(savedInstanceState); 71 setContentView(R.layout.activity_main); 72 init(); 73 } 74 75 /** 76 * 初始化操作 77 */ 78 public void init() { 79 final Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); 80 downloadUrlEt = (EditText) findViewById(R.id.url_et); 81 downloadUrlEt.setText("http://mit.95195.com/singleonline.apk"); 82 downloadBtn = (Button) findViewById(R.id.downloadBtn); 83 pauseBtn = (Button) findViewById(R.id.pauseBtn); 84 85 downloadBtn.setOnClickListener(new OnClickListener() { 86 @Override 87 public void onClick(View view) { 88 String url = downloadUrlEt.getText().toString(); 89 if (url.equals("") || url == null) { 90 v.vibrate(100); 91 Toast.makeText(MainActivity.this, "请输入正确的地址", 92 Toast.LENGTH_SHORT).show(); 93 downloadUrlEt.setText(""); 94 return; 95 } 96 97 download(url); 98 downloadBtn.setVisibility(View.GONE); 99 }100 });101 102 callBack = new AjaxCallBack () {103 104 @Override105 public void onFailure(Throwable t, int errorNo, String strMsg) { // 下载失败106 super.onFailure(t, errorNo, strMsg);107 Log.i(TAG, "下载失败..." + t.getStackTrace() + strMsg);108 109 }110 111 @Override112 public void onStart() { // 开始下载113 super.onStart();114 Log.i(TAG, "开始下载...");115 sendNotification();116 }117 118 @Override119 public void onSuccess(File t) { // 下载成功120 super.onSuccess(t);121 Log.i(TAG, "下载完成...");122 manager.cancel(0);123 AlertDialog dialog = new AlertDialog.Builder(MainActivity.this).setTitle("下载完成该...").setPositiveButton("确定", new DialogInterface.OnClickListener() {124 125 @Override126 public void onClick(DialogInterface arg0, int arg1) {127 downloadBtn.setVisibility(View.GONE);128 pauseBtn.setVisibility(View.GONE);129 }130 }).create();131 }132 133 @Override134 public void onLoading(long count, long current) { // 正在下载135 super.onLoading(count, current);136 Log.i(TAG, "progress = " + progress);137 if (current != count && current != 0) {138 progress = (int) (current / (float) count * 100);139 } else {140 progress = 100;141 }142 handler.handleMessage(msg);143 }144 145 };146 147 pauseBtn.setOnClickListener(new OnClickListener() {148 149 @Override150 public void onClick(View view) {151 152 if(isPaused){153 isPaused = false;154 callBack.progress(true, (int)progress);155 pauseBtn.setText("暂停下载...");156 }else{157 callBack.progress(false, (int)progress);158 pauseBtn.setText("继续下载");159 isPaused = true;160 }161 162 }163 });164 165 manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);166 notification = new Notification(R.drawable.ic_launcher, "下载进度条...",System.currentTimeMillis());167 168 }169 170 /**171 * 判断SD卡是否可用172 * 173 * @return174 */175 public boolean isExternalStorageAvaliable() {176 177 String state = Environment.getExternalStorageState();178 179 if (Environment.MEDIA_MOUNTED.equals(state)) {180 return true;181 } else {182 Toast.makeText(this, "未检测到SD卡...", Toast.LENGTH_SHORT).show();183 return false;184 }185 186 }187 188 /**189 * 从指定的地址下载文件190 * 191 * @param url192 * 下载地址193 */194 public void download(String url) {195 196 FinalHttp http = new FinalHttp();197 if (!isExternalStorageAvaliable()) {198 return;199 }200 String apkPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/single.apk";201 File f = new File(apkPath);202 if(f.exists()){203 f.delete();204 }205 206 http.download(url, apkPath, callBack);207 }208 209 /**210 * 发送通知211 */212 @SuppressWarnings("deprecation")213 public void sendNotification() {214 contentView = new RemoteViews(getPackageName(), R.layout.notify_view);215 contentView.setProgressBar(R.id.pb, 100, 0, false);216 notification.contentView = contentView;217 manager.notify(0, notification);218 }219 220 @Override221 public boolean onCreateOptionsMenu(Menu menu) {222 getMenuInflater().inflate(R.menu.main, menu);223 return true;224 }225 226 /**227 * 当界面停止的时候取消下载228 */229 @Override230 protected void onPause() {231 manager.cancel(0);232 super.onPause();233 }234 235 }
activity_main.xml
17 8 14 15 22 23 30 31 38 39
notify_view.xml
1 25 6 11 12 19 20 32 33
代码下载地址: