Android 中 Service+Notification 斷點續傳下載

DylanAndroid發表於2019-03-04

轉載請註明出處:http://blog.csdn.net/linglongxin24/article/details/53874148
本文出自【DylanAndroid的部落格】


#Android中Service+Notification斷點續傳下載

在Android開發中,我們經常會用到例如版本更新這種的檔案下載,那麼如何用service+notification去做一個支援斷點續傳的
檔案下載呢?效果圖如下

Android 中 Service+Notification 斷點續傳下載
效果圖

#一.在啟動Activity中啟動Service

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的啟動命令中註冊按鈕點選廣播


    @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.開始下載用xutils3

    /**
     * 下載檔案
     */
    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.顯示通知


    /**
     * 顯示一個下載帶進度條的通知
     *
     * @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.更新通知欄狀態


    /**
     * 在通知欄顯示檔名
     *
     * @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完整程式碼

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();
    }

}複製程式碼

8.GitHub

相關文章