轉載請註明出處:http://blog.csdn.net/linglongxin24/article/details/53874148
本文出自【DylanAndroid的博客】 php
#Android中Service+Notification斷點續傳下載java
在Android開發中,咱們常常會用到例如版本更新這種的文件下載,那麼如何用service+notification去作一個支持斷點續傳的
文件下載呢?效果圖以下android
#一.在啓動Activity中啓動Servicegit
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, TaskService.class);
startService(intent);
}
}複製代碼
#2.在Service的啓動命令中註冊按鈕點擊廣播github
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
registerBroadCast();
download();
return super.onStartCommand(intent, flags, startId);
}
/** * 註冊按鈕點擊廣播* */
private void registerBroadCast() {
myBroadcastReceiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(BROADCAST_ACTION_CLICK);
registerReceiver(myBroadcastReceiver, filter);
}
/** * 更新通知界面的按鈕的廣播 */
private class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (!intent.getAction().equals(BROADCAST_ACTION_CLICK)) {
return;
}
Logger.d("status=" + status);
switch (status) {
case DOWNLOADING:
/**當在下載中點擊暫停按鈕**/
cancelable.cancel();
mRemoteViews.setTextViewText(R.id.bt, "下載");
mRemoteViews.setTextViewText(R.id.tv_message, "暫停中...");
status = Status.PAUSE;
notificationManager.notify(NOTIFICATION_ID, notification);
break;
case SUCCESS:
/**當下載完成點擊完成按鈕時關閉通知欄**/
notificationManager.cancel(NOTIFICATION_ID);
break;
case FAIL:
case PAUSE:
/**當在暫停時點擊下載按鈕**/
download();
mRemoteViews.setTextViewText(R.id.bt, "暫停");
mRemoteViews.setTextViewText(R.id.tv_message, "下載中...");
status = Status.DOWNLOADING;
notificationManager.notify(NOTIFICATION_ID, notification);
break;
}
}
}複製代碼
#3.開始下載用xutils3app
/** * 下載文件 */
private void download() {
final String url = "https://github.com/linglongxin24/DylanStepCount/raw/master/app-debug.apk";
RequestParams requestParams = new RequestParams(url);
String fileName = url.substring(url.lastIndexOf("/") + 1);
file = new File(filePath, fileName);
showNotificationProgress(TaskService.this);
showFileName(fileName);
requestParams.setSaveFilePath(file.getPath());
/**自動爲文件命名**/
requestParams.setAutoRename(true);
/**自動爲文件斷點續傳**/
requestParams.setAutoResume(true);
cancelable = x.http().get(requestParams, new Callback.ProgressCallback<File>() {
@Override
public void onSuccess(File result) {
Logger.d("下載完成");
Logger.d("result=" + result.getPath());
downloadSuccess();
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
Logger.d("下載異常");
downloadFail();
}
@Override
public void onCancelled(CancelledException cex) {
Logger.d("下載已取消");
}
@Override
public void onFinished() {
}
@Override
public void onWaiting() {
}
@Override
public void onStarted() {
}
@Override
public void onLoading(long total, long current, boolean isDownloading) {
Logger.d("total=" + total + "--" + "current=" + current);
updateNotification(total, current);
}
});
}複製代碼
#4.顯示通知ide
/** * 顯示一個下載帶進度條的通知 * * @param context 上下文 */
public void showNotificationProgress(Context context) {
/**進度條通知構建**/
NotificationCompat.Builder builderProgress = new NotificationCompat.Builder(context);
/**設置爲一個正在進行的通知**/
builderProgress.setOngoing(true);
/**設置小圖標**/
builderProgress.setSmallIcon(R.mipmap.ic_launcher);
/**新建通知自定義佈局**/
mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.notification);
/**進度條ProgressBar**/
mRemoteViews.setProgressBar(R.id.pb, 100, 0, false);
/**提示信息的TextView**/
mRemoteViews.setTextViewText(R.id.tv_message, "下載中...");
/**操做按鈕的Button**/
mRemoteViews.setTextViewText(R.id.bt, "暫停");
/**設置左側小圖標*/
mRemoteViews.setImageViewResource(R.id.iv, R.mipmap.ic_launcher);
/**設置經過廣播形式的PendingIntent**/
Intent intent = new Intent(BROADCAST_ACTION_CLICK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE_BROADCAST, intent, 0);
mRemoteViews.setOnClickPendingIntent(R.id.bt, pendingIntent);
/**設置自定義佈局**/
builderProgress.setContent(mRemoteViews);
/**設置滾動提示**/
builderProgress.setTicker("開始下載...");
notification = builderProgress.build();
/**設置不可手動清除**/
notification.flags = Notification.FLAG_NO_CLEAR;
/**獲取通知管理器**/
notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
/**發送一個通知**/
notificationManager.notify(NOTIFICATION_ID, notification);
}複製代碼
#5.顯示通知的自定義佈局佈局
<?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="60dp" android:orientation="horizontal">
<ImageView android:id="@+id/iv" android:layout_width="40dp" android:layout_height="40dp" />
<LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_weight="1" android:orientation="vertical">
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="隨時隨地" android:textColor="@color/colorAccent" android:textSize="12sp" />
</LinearLayout>
<ProgressBar android:id="@+id/pb" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="20dp" android:max="100" android:progressBackgroundTint="@color/colorPrimaryDark" android:progress="50" />
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
<TextView android:id="@+id/tv_message" android:layout_width="wrap_content" android:layout_height="match_parent" android:text="正在下載中..." android:textColor="@color/colorAccent" android:textSize="12sp" />
<TextView android:id="@+id/tv_progress" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:layout_gravity="center_vertical" android:gravity="center" android:textColor="@color/colorAccent" android:textSize="12sp" />
<TextView android:id="@+id/tv_size" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/colorAccent" android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
<Button android:id="@+id/bt" android:layout_width="50dp" android:layout_height="30dp" android:layout_gravity="center_vertical" android:gravity="center" android:text="下載" android:textColor="@color/colorAccent" android:textSize="12sp" />
</LinearLayout>複製代碼
#6.更新通知欄狀態ui
/** * 在通知欄顯示文件名 * * @param url 下載地址 */
private void showFileName(String url) {
mRemoteViews.setTextViewText(R.id.tv_name, url.substring(url.lastIndexOf("/") + 1));
notificationManager.notify(NOTIFICATION_ID, notification);
}
/** * 下載更改進度 * * @param total 總大小 * @param current 當前已下載大小 */
private void updateNotification(long total, long current) {
mRemoteViews.setTextViewText(R.id.tv_size, formatSize(current) + "/" + formatSize(total));
int result = Math.round((float) current / (float) total * 100);
mRemoteViews.setTextViewText(R.id.tv_progress, result + "%");
mRemoteViews.setProgressBar(R.id.pb, 100, result, false);
notificationManager.notify(NOTIFICATION_ID, notification);
}
/** * 下載失敗 */
private void downloadFail() {
status = Status.FAIL;
if (!cancelable.isCancelled()) {
cancelable.cancel();
}
mRemoteViews.setTextViewText(R.id.bt, "重試");
mRemoteViews.setTextViewText(R.id.tv_message, "下載失敗");
notificationManager.notify(NOTIFICATION_ID, notification);
}
/** * 下載成功 */
private void downloadSuccess() {
status = Status.SUCCESS;
mRemoteViews.setTextViewText(R.id.bt, "完成");
mRemoteViews.setTextViewText(R.id.tv_message, "下載完成");
notificationManager.notify(NOTIFICATION_ID, notification);
}複製代碼
#7.TaskService完整代碼this
package cn.bluemobi.dylan.servicetask;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Environment;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.app.NotificationCompat;
import android.widget.RemoteViews;
import com.orhanobut.logger.Logger;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;
import java.io.File;
/** * Created by yuandl on 2016-12-19. */
public class TaskService extends Service {
/**** * 發送廣播的請求碼 */
private final int REQUEST_CODE_BROADCAST = 0X0001;
/**** * 發送廣播的action */
private final String BROADCAST_ACTION_CLICK = "servicetask";
/** * 通知 */
private Notification notification;
/** * 通知的Id */
private final int NOTIFICATION_ID = 1;
/** * 通知管理器 */
private NotificationManager notificationManager;
/** * 通知欄的遠程View */
private RemoteViews mRemoteViews;
/** * 下載是否可取消 */
private Callback.Cancelable cancelable;
/** * 自定義保存路徑,Environment.getExternalStorageDirectory():SD卡的根目錄 */
private String filePath = Environment.getExternalStorageDirectory() + "/ServiceTask/";
private File file;
/** * 通知欄操做的四種狀態 */
private enum Status {
DOWNLOADING, PAUSE, FAIL, SUCCESS
}
/** * 當前在狀態 默認正在下載中 */
private Status status = Status.DOWNLOADING;
private MyBroadcastReceiver myBroadcastReceiver;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
registerBroadCast();
download();
return super.onStartCommand(intent, flags, startId);
}
/** * 註冊按鈕點擊廣播* */
private void registerBroadCast() {
myBroadcastReceiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(BROADCAST_ACTION_CLICK);
registerReceiver(myBroadcastReceiver, filter);
}
/** * 更新通知界面的按鈕的廣播 */
private class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (!intent.getAction().equals(BROADCAST_ACTION_CLICK)) {
return;
}
Logger.d("status=" + status);
switch (status) {
case DOWNLOADING:
/**當在下載中點擊暫停按鈕**/
cancelable.cancel();
mRemoteViews.setTextViewText(R.id.bt, "下載");
mRemoteViews.setTextViewText(R.id.tv_message, "暫停中...");
status = Status.PAUSE;
notificationManager.notify(NOTIFICATION_ID, notification);
break;
case SUCCESS:
/**當下載完成點擊完成按鈕時關閉通知欄**/
notificationManager.cancel(NOTIFICATION_ID);
break;
case FAIL:
case PAUSE:
/**當在暫停時點擊下載按鈕**/
download();
mRemoteViews.setTextViewText(R.id.bt, "暫停");
mRemoteViews.setTextViewText(R.id.tv_message, "下載中...");
status = Status.DOWNLOADING;
notificationManager.notify(NOTIFICATION_ID, notification);
break;
}
}
}
/** * 下載文件 */
private void download() {
final String url = "https://github.com/linglongxin24/DylanStepCount/raw/master/app-debug.apk";
RequestParams requestParams = new RequestParams(url);
String fileName = url.substring(url.lastIndexOf("/") + 1);
file = new File(filePath, fileName);
showNotificationProgress(TaskService.this);
showFileName(fileName);
requestParams.setSaveFilePath(file.getPath());
/**自動爲文件命名**/
requestParams.setAutoRename(true);
/**自動爲文件斷點續傳**/
requestParams.setAutoResume(true);
cancelable = x.http().get(requestParams, new Callback.ProgressCallback<File>() {
@Override
public void onSuccess(File result) {
Logger.d("下載完成");
Logger.d("result=" + result.getPath());
downloadSuccess();
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
Logger.d("下載異常");
downloadFail();
}
@Override
public void onCancelled(CancelledException cex) {
Logger.d("下載已取消");
}
@Override
public void onFinished() {
}
@Override
public void onWaiting() {
}
@Override
public void onStarted() {
}
@Override
public void onLoading(long total, long current, boolean isDownloading) {
Logger.d("total=" + total + "--" + "current=" + current);
updateNotification(total, current);
}
});
}
/** * 顯示一個下載帶進度條的通知 * * @param context 上下文 */
public void showNotificationProgress(Context context) {
/**進度條通知構建**/
NotificationCompat.Builder builderProgress = new NotificationCompat.Builder(context);
/**設置爲一個正在進行的通知**/
builderProgress.setOngoing(true);
/**設置小圖標**/
builderProgress.setSmallIcon(R.mipmap.ic_launcher);
/**新建通知自定義佈局**/
mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.notification);
/**進度條ProgressBar**/
mRemoteViews.setProgressBar(R.id.pb, 100, 0, false);
/**提示信息的TextView**/
mRemoteViews.setTextViewText(R.id.tv_message, "下載中...");
/**操做按鈕的Button**/
mRemoteViews.setTextViewText(R.id.bt, "暫停");
/**設置左側小圖標*/
mRemoteViews.setImageViewResource(R.id.iv, R.mipmap.ic_launcher);
/**設置經過廣播形式的PendingIntent**/
Intent intent = new Intent(BROADCAST_ACTION_CLICK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, REQUEST_CODE_BROADCAST, intent, 0);
mRemoteViews.setOnClickPendingIntent(R.id.bt, pendingIntent);
/**設置自定義佈局**/
builderProgress.setContent(mRemoteViews);
/**設置滾動提示**/
builderProgress.setTicker("開始下載...");
notification = builderProgress.build();
/**設置不可手動清除**/
notification.flags = Notification.FLAG_NO_CLEAR;
/**獲取通知管理器**/
notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
/**發送一個通知**/
notificationManager.notify(NOTIFICATION_ID, notification);
}
/** * 在通知欄顯示文件名 * * @param url 下載地址 */
private void showFileName(String url) {
mRemoteViews.setTextViewText(R.id.tv_name, url.substring(url.lastIndexOf("/") + 1));
notificationManager.notify(NOTIFICATION_ID, notification);
}
/** * 下載更改進度 * * @param total 總大小 * @param current 當前已下載大小 */
private void updateNotification(long total, long current) {
mRemoteViews.setTextViewText(R.id.tv_size, formatSize(current) + "/" + formatSize(total));
int result = Math.round((float) current / (float) total * 100);
mRemoteViews.setTextViewText(R.id.tv_progress, result + "%");
mRemoteViews.setProgressBar(R.id.pb, 100, result, false);
notificationManager.notify(NOTIFICATION_ID, notification);
}
/** * 下載失敗 */
private void downloadFail() {
status = Status.FAIL;
if (!cancelable.isCancelled()) {
cancelable.cancel();
}
mRemoteViews.setTextViewText(R.id.bt, "重試");
mRemoteViews.setTextViewText(R.id.tv_message, "下載失敗");
notificationManager.notify(NOTIFICATION_ID, notification);
}
/** * 下載成功 */
private void downloadSuccess() {
status = Status.SUCCESS;
mRemoteViews.setTextViewText(R.id.bt, "完成");
mRemoteViews.setTextViewText(R.id.tv_message, "下載完成");
notificationManager.notify(NOTIFICATION_ID, notification);
}
/** * 格式化文件大小 * * @param size * @return */
private String formatSize(long size) {
String format;
if (size >= 1024 * 1024) {
format = byteToMB(size) + "M";
} else if (size >= 1024) {
format = byteToKB(size) + "k";
} else {
format = size + "b";
}
return format;
}
/** * byte轉換爲MB * * @param bt 大小 * @return MB */
private float byteToMB(long bt) {
int mb = 1024 * 1024;
float f = (float) bt / (float) mb;
float temp = (float) Math.round(f * 100.0F);
return temp / 100.0F;
}
/** * byte轉換爲KB * * @param bt 大小 * @return K */
private int byteToKB(long bt) {
return Math.round((bt / 1024));
}
/** * 銷燬時取消下載,並取消註冊廣播,防止內存溢出 */
@Override
public void onDestroy() {
if (cancelable != null && !cancelable.isCancelled()) {
cancelable.cancel();
}
if (myBroadcastReceiver != null) {
unregisterReceiver(myBroadcastReceiver);
}
super.onDestroy();
}
}複製代碼