Service服務實現下載功能

Service生命週期最全面解
http://www.jianshu.com/p/8d0cde35eb10

Service服務史上最全面解析
http://www.jianshu.com/p/d963c55c3ab9

實例

這裏寫圖片描述


DwonloadListener.class
下載過程中的各狀態進行監聽和回調

public interface DownloadListener {

    //下載進度
    void onProgress(int progress);

    //下載成功
    void onSuccess();

    //下載失敗
    void onFailed();

    //下載暫停
    void onPaused();

    //下載取消
    void onCanceled();
}

DownloadTask.class
下載類

public class DownloadTask extends AsyncTask <String,Integer,Integer>{

    public static final int TYPE_SUCCES = 0;

    public static final int TYPE_FAILED = 1;

    public static final int TYPE_PAUSED = 2;

    public static final int TYPE_CANCELED = 3;

    private DownloadListener listener;

    private boolean isCanceled = false;

    private boolean isPaused = false;

    private int lastProgress;

    public DownloadTask(DownloadListener listener){
        //接收一個接口實現類
        this.listener = listener;
    }

    //下載邏輯
    @Override
    protected Integer doInBackground(String... params) {
        InputStream is = null;
        //RandomAccessFile 是隨機訪問文件(包括讀/寫)的類。
        //它支持對文件隨機訪問的讀取和寫入,即我們可以從指定的位置讀取/寫入文件數據。
        //如果要想對文件內容進行操作,則可以使用RandomAccessFile類
        RandomAccessFile savedFile = null;
        File file = null;
        try{
            long downloadedLength = 0;
            String downloadUrl = params[0];
            String fileName = downloadUrl.substring(downloadUrl.lastIndexOf("/"));
            String directory = Environment.getExternalStoragePublicDirectory
                    (Environment.DIRECTORY_DOWNLOADS).getPath();
            file = new File(directory + fileName);
            if(file.exists()){
                //已下載的文件字節長度
                downloadedLength = file.length();
            }

            //服務器文件字節長度
            long contentLength = getContentLength(downloadUrl);
            if(contentLength == 0){
                //如果服務器文件字節長度等於0,說明下載文件有問題,下載失敗
                return TYPE_FAILED;
            }else if(contentLength == downloadedLength){
                //如果服務器文件字節長度等於已下載文件字節長度,說明已經下載完成
                return TYPE_SUCCES;
            }
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder()
                     //斷點下載,指定從哪個字節開始下載
                    .addHeader("RANGE","bytes="+ downloadedLength + "-")
                    .url(downloadUrl)
                    .build();
            Response response = client.newCall(request).execute();
            if(response != null){
                //剩下的字節
                is = response.body().byteStream();
                //只讀方式("r"),還是以讀寫方式("rw")
                savedFile = new RandomAccessFile(file,"rw");
                //在文件裏移動用的seek( ),skipBytes()跳過多少字節數,定位用的getFilePointer( )。
                savedFile.seek(downloadedLength);
                byte[] b = new byte[1024];
                int total = 0;
                int len;
                while((len=is.read(b))!=-1){
                    if(isCanceled) { //取消
                        return TYPE_CANCELED;
                    } else if(isPaused) {//暫停
                        return TYPE_PAUSED;
                    } else {
                        total += len;
                        savedFile.write(b,0,len);
                        //下載百分比
                        int progress = (int)((total + downloadedLength) * 100/contentLength);
                        publishProgress(progress);
                    }
                }
                response.body().close();
                return TYPE_SUCCES;
            }
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            try{
                if(is != null){
                    is.close();
                }

                if(savedFile !=null){
                    savedFile.close();
                }

                if(isCanceled && file!=null){
                    //如果取消,文件不等於null就刪除
                    file.delete();
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }
        return TYPE_FAILED;
    }

    //在界面上更新下載進度
    @Override
    protected void onProgressUpdate(Integer... values) {

        int progress = values[0];
        if(progress > lastProgress){//和上次的進度比較,如果大於上次的進度通知更新
            //進行回調,在實現類進行更新通知
            listener.onProgress(progress);
            lastProgress = progress;
        }
    }

    //通知最後下載結果
    @Override
    protected void onPostExecute(Integer status) {
        switch (status){
            case TYPE_SUCCES:
                listener.onSuccess();
                break;
            case TYPE_FAILED:
                listener.onFailed();
                break;
            case TYPE_PAUSED:
                listener.onPaused();
                break;
            case TYPE_CANCELED:
                listener.onCanceled();
                break;
            default:
                break;
        }
    }

    //暫停
    public void pauseDownload(){
        isPaused = true;
    }

    //取消
    public void cancelDownload(){
        isCanceled = true;
    }

    //
    private long getContentLength(String downloadUrl) throws IOException {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(downloadUrl)
                .build();
        Response response = client.newCall(request).execute();
        if(response != null && response.isSuccessful()){
            long contentLength = response.body().contentLength();
            response.body().close();
            return contentLength;
        }
        return 0;
    }
}

DownloadService .class
服務類

public class DownloadService extends Service {

    private DownloadTask downloadTask;

    private String downloadUrl;

    //實現回調接口,DownloadTask類在下載過程中的各狀態進行監聽和回調更新通知
    private DownloadListener listener = new DownloadListener() {

        //下載進度
        @Override
        public void onProgress(int progress) {
            getNotificationManager().notify(1, getNotification("Downloading...",progress));
        }

        //下載成功
        @Override
        public void onSuccess() {
            downloadTask = null;
            //下載成功將前臺服務通知關閉
            stopForeground(true);
            //並創建一個下載成功的通知
            getNotificationManager().notify(1,getNotification("Download Success",-1));
            Toast.makeText(DownloadService.this, "Download Success", Toast.LENGTH_SHORT).show();
        }

        //下載失敗
        @Override
        public void onFailed() {
            downloadTask = null;
            //下載失敗將前臺服務通知關閉
            stopForeground(true);
            //並創建一個下載失敗的通知
            getNotificationManager().notify(1,getNotification("Download Failed",-1));
            Toast.makeText(DownloadService.this, "Download Failed", Toast.LENGTH_SHORT).show();
        }

        //下載暫停
        @Override
        public void onPaused() {
            downloadTask = null;
            Toast.makeText(DownloadService.this, "Paused",Toast.LENGTH_SHORT).show();
        }

        //下載取消
        @Override
        public void onCanceled() {
            downloadTask = null;
            stopForeground(true);
            Toast.makeText(DownloadService.this, "Canceled",Toast.LENGTH_SHORT).show();
        }
    };

    private DownloadBinder mBinder = new DownloadBinder();

    //當活動綁定服務的時候會返回給活動一個mBinder實例
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return mBinder;
    }

    //活動和服務進行通信的類,活動可以對下載功能進行管理
    class DownloadBinder extends Binder {

        //開始下載
        public void startDownload(String url){
            if(downloadTask == null){
                downloadUrl = url;
                //傳入DownloadListener接口的實現類,DownloadTask才能進行回調更新通知
                downloadTask = new DownloadTask(listener);
                downloadTask.execute(downloadUrl);
                startForeground(1,getNotification("Download...",0));
                Toast.makeText(DownloadService.this, "Download...",Toast.LENGTH_SHORT).show();
            }
        }

        //暫停下載
        public void pauseDownload(){
            if(downloadTask != null){
                downloadTask.pauseDownload();
            }
        }

        //取消下載
        public void cancelDownload(){
            if(downloadTask != null){
                downloadTask.cancelDownload();

            } else if(downloadUrl != null){
                //取消下載時需將文件刪除,並將通知關閉
                String fileName = downloadUrl.substring(downloadUrl.lastIndexOf("/"));
                String directory = Environment.getExternalStoragePublicDirectory
                        (Environment.DIRECTORY_DOCUMENTS).getPath();
                File file = new File(directory + fileName);
                if(file.exists()){
                    file.delete();
                }
                getNotificationManager().cancel(1);
                stopForeground(true);
                Toast.makeText(DownloadService.this, "Delete File",Toast.LENGTH_SHORT).show();
            }
        }

    }

    //獲取系統通知服務
    private NotificationManager getNotificationManager(){
        return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }

    //創建通知
    private Notification getNotification(String title, int progress){
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentTitle(title);
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher));
        builder.setContentIntent(pi);

        if(progress > 0){
            builder.setContentText(progress + "%");
            builder.setProgress(100, progress,false);
        }
        return builder.build();
    }


}

MainActivity.class

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private DownloadService.DownloadBinder downloadBinder;

    private ServiceConnection connection = new ServiceConnection() {
        //綁定服務調用
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder service) {
            downloadBinder = (DownloadService.DownloadBinder) service;
        }

        //解綁服務調用
        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button startDownload = (Button) findViewById(R.id.start_download);
        Button pauseDownload = (Button) findViewById(R.id.pause_download);
        Button cancelDownload = (Button) findViewById(R.id.cancel_donwload);
        startDownload.setOnClickListener(this);
        pauseDownload.setOnClickListener(this);
        cancelDownload.setOnClickListener(this);

        Intent intent = new Intent(this, DownloadService.class);
        startService(intent);//啓動服務
        bindService(intent, connection, BIND_AUTO_CREATE);//綁定服務

        if(ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
        }
    }

    @Override
    public void onClick(View view) {
        if(downloadBinder == null){
            return;
        }
        switch (view.getId()){
            case R.id.start_download:
                String url = "https://raw.githubusercontent.com/guolindev/eclipse/master/eclipse-inst-win64.exe";
                downloadBinder.startDownload(url);
                break;
            case R.id.pause_download:
                downloadBinder.pauseDownload();
                break;
            case R.id.cancel_donwload:
                downloadBinder.cancelDownload();
                break;
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode){
            case 1:
                if(grantResults.length > 0 && grantResults[0] != PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(this, "拒絕權限將無法使用程序",Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
            default:
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(connection);//解綁服務
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章