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 AjaxCallBack<File> callBack; 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<File>() { 103 104 @Override 105 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 @Override 112 public void onStart() {// 开始下载 113 super.onStart(); 114 Log.i(TAG, "开始下载..."); 115 sendNotification(); 116 } 117 118 @Override 119 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 @Override 126 public void onClick(DialogInterface arg0, int arg1) { 127 downloadBtn.setVisibility(View.GONE); 128 pauseBtn.setVisibility(View.GONE); 129 } 130 }).create(); 131 } 132 133 @Override 134 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 @Override 150 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 * @return 174 */ 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 url 192 * 下载地址 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 @Override 221 public boolean onCreateOptionsMenu(Menu menu) { 222 getMenuInflater().inflate(R.menu.main, menu); 223 return true; 224 } 225 226 /** 227 * 当界面停止的时候取消下载 228 */ 229 @Override 230 protected void onPause() { 231 manager.cancel(0); 232 super.onPause(); 233 } 234 235 }
activity_main.xml
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:padding="10dp" 6 tools:context=".MainActivity" > 7 8 <TextView 9 android:id="@+id/hint_tv" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:paddingTop="10dp" 13 android:text="@string/tv_main_hint" /> 14 15 <EditText 16 android:id="@+id/url_et" 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" 19 android:layout_toRightOf="@id/hint_tv" 20 android:hint="@string/et_downloadurl" 21 android:singleLine="true" /> 22 23 <Button 24 android:id="@+id/downloadBtn" 25 android:layout_width="match_parent" 26 android:layout_height="wrap_content" 27 android:layout_below="@id/url_et" 28 android:paddingTop="20dp" 29 android:text="@string/str_download" /> 30 31 <Button 32 android:layout_below="@id/downloadBtn" 33 android:id="@+id/pauseBtn" 34 android:layout_width="match_parent" 35 android:layout_height="wrap_content" 36 android:paddingTop="20dp" 37 android:text="@string/btn_pause" /> 38 39 </RelativeLayout>
notify_view.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent" > 5 6 <ImageView 7 android:id="@+id/notify_icon" 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 android:src="@drawable/ic_launcher" /> 11 12 <TextView 13 android:id="@+id/tv" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:layout_toRightOf="@id/notify_icon" 17 android:singleLine="true" 18 android:text="下载进度..." /> 19 20 <ProgressBar 21 android:id="@+id/pb" 22 style="?android:attr/progressBarStyleHorizontal" 23 android:layout_width="match_parent" 24 android:layout_height="wrap_content" 25 android:layout_below="@id/tv" 26 android:layout_toRightOf="@id/notify_icon" 27 android:max="100" 28 android:paddingRight="10dp" 29 android:progress="50" 30 android:secondaryProgress="74" 31 android:visibility="visible" /> 32 33 </RelativeLayout>
代码下载地址:http://download.csdn.net/detail/yuan936845015/6444679
网友 2014/4/21 19:11:18 发表
你在里的暂停继续下载不能起到作用吧