第一行代碼(十)

第十章主要講 Android 中四大組件的服務(wù)

一、什么是服務(wù)

??服務(wù)是 Android 中實(shí)現(xiàn)程序后臺(tái)運(yùn)行的解決方案,適合去執(zhí)行那些不需要和用戶交互而且要長期運(yùn)行的任務(wù),即使程序被切換到后臺(tái),或者用戶打開了另一個(gè)應(yīng)用程序,服務(wù)仍然能夠保持正常運(yùn)行。

注意:服務(wù)并不是運(yùn)行在一個(gè)獨(dú)立的進(jìn)程當(dāng)中,而是依賴于創(chuàng)建服務(wù)時(shí)所在的應(yīng)用程序進(jìn)程,當(dāng)某個(gè)應(yīng)用程序被進(jìn)程殺掉時(shí),所有依賴于該進(jìn)程的服務(wù)也會(huì)停止運(yùn)行。另外,服務(wù)并不會(huì)自動(dòng)開啟線程,所有的代碼都是默認(rèn)運(yùn)行在主線程當(dāng)中的,所以如果要進(jìn)行耗時(shí)操作,我們需要在服務(wù)的內(nèi)部手動(dòng)創(chuàng)建子線程,否則就有可能出現(xiàn)主線程被阻塞的情況。

二、Android 多線程編程

??新建一個(gè)類繼承自 Thread,然后重寫 run()方法

public class MyThread extends Thread {

    @Override
    public void run() {
        super.run();
    }
    
}

new MyThread().start();

但是使用繼承的方式耦合性有點(diǎn)高,更多的時(shí)候我們會(huì)選擇實(shí)現(xiàn) Runnable 接口的方式來定義一個(gè)線程

public class MyThread implements Runnable {

    @Override
    public void run() {
        
    }
}
        MyThread myThread = new MyThread();
        new Thread(myThread).start();

??知道了如何開啟線程后,我們需要注意,Android 的 UI 是線程不安全的,因此,如果想要更新應(yīng)用程序里的 UI 元素,必須在主線程中進(jìn)行,否則會(huì)出現(xiàn)異常。如果在子線程中進(jìn)行了 UI 更新的操作,會(huì)報(bào)錯(cuò)。
??對(duì)于這種情況,Android 提供了一套異步消息處理機(jī)制,完美地解決了在子線程中進(jìn)行 UI 操作的問題。

三、異步消息處理機(jī)制

        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Message msg = Message.obtain();
                        msg.what = UPDATE_TEXT;
                        handler.sendMessage(msg);
                    }
                }).start();
            }
        });
    public static final int UPDATE_TEXT = 1;

    private Handler handler = new Handler() {
        /**
         * 該方法是運(yùn)行在主線程當(dāng)中的
         */
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case UPDATE_TEXT:
                    tv.setText("這里是主線程,可以進(jìn)行 UI 操作");
                    break;
                default:
                    break;
            }
        }
    };

??上面是異步消息的代碼,下面來解析異步消息處理機(jī)制。Android 中的異步消息處理主要由4個(gè)部分組成:Message、Handler、MessageQueue 和 Looper

  • Message:
    ??Message 是在線程之間傳遞的消息,內(nèi)部可攜帶少量的信息,Message 不僅可以使用 what 字段,還可以使用 arg1和 arg2字段來攜帶一些整形數(shù)據(jù),還可以使用 obj 字段攜帶一個(gè) Object 對(duì)象。
  • Handler:
    ??顧名思義,是處理者的意思,主要用于發(fā)送和處理消息,發(fā)送消息一般使用 Handler 的 sendMessage()方法,發(fā)出的消息經(jīng)過輾轉(zhuǎn)處理后,最終會(huì)傳遞到 Handler 的 handleMessage()方法中。
  • MessageQueue:
    ??是消息隊(duì)列的意思,主要用于存放所有通過 Handler 發(fā)送的消息,這部分消息一直會(huì)存在于消息隊(duì)列中,等待被處理,每個(gè)線程中只會(huì)有一個(gè) MessageQueue 對(duì)象。
  • Looper:
    ??Looper 是每個(gè)線程中的 MessageQueue 的管家,調(diào)用 Looper 的 loop()方法后,就會(huì)進(jìn)入到一個(gè)無限循環(huán)當(dāng)中,然后每當(dāng)發(fā)現(xiàn) MessageQueue 中存在一條消息,就會(huì)將它取出,并傳遞到 Handler 的 handleMessage()方法中,每個(gè)線程中也只會(huì)有一個(gè) Looper 對(duì)象。

整體梳理:首先在主線程中創(chuàng)建一個(gè) Handler 對(duì)象,并重寫 handleMessage()方法,然后當(dāng)子線程中需要進(jìn)行 UI 操作時(shí),就創(chuàng)建一個(gè) Message 對(duì)象,并通過 Handler 將這條消息發(fā)送出去。之后這條消息會(huì)被添加到 MessageQueue 的隊(duì)列中等待被處理,而 Looper 則會(huì)一直嘗試從 MessageQueue 中取出待處理的消息,然后分發(fā)回 Handler 的 handleMessage()方法中。由于 Handler 是在主線程中創(chuàng)建的,所以此時(shí) handleMessage()方法中的代碼也會(huì)在主線程中運(yùn)行。

我們以前使用到的 runOnUiThread()方法其實(shí)就是一個(gè)異步消息處理機(jī)制的接口封裝。

四、使用 AsyncTask

??AsyncTask 背后的實(shí)現(xiàn)原理也是基于異步消息處理機(jī)制的,只是 Android 幫我們做了很好的封裝而已,AsyncTask 是一個(gè)抽象類,所以我們要?jiǎng)?chuàng)建一個(gè)子類去繼承它,在繼承的時(shí)候可以為 AsyncTask 指定3個(gè)泛型參數(shù)(如果不需要,可以寫 Void):

  1. Params:在執(zhí)行 AsyncTask 時(shí)傳入的參數(shù),可用于在后臺(tái)任務(wù)中使用。

2.Progress:后臺(tái)任務(wù)執(zhí)行時(shí),如果需要在界面上顯示當(dāng)前的進(jìn)度,則使用這里指定的泛型作為進(jìn)度單位。

3.Result:當(dāng)任務(wù)執(zhí)行完畢后,如果需要對(duì)結(jié)果進(jìn)行返回,則使用這里指定的泛型作為返回值類型。

public class DownloadTask extends AsyncTask<Void,Integer,Boolean> {

    /**
     * 在后臺(tái)任務(wù)開始執(zhí)行之前調(diào)用,用于進(jìn)行一些界面上的初始化操作
     */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    /**
     * 該方法中的所有代碼都會(huì)在子線程中運(yùn)行,任務(wù)一旦完成就可以通過
     * return 語句來將任務(wù)的執(zhí)行結(jié)果返回,如果 AsyncTask 的第三個(gè)
     * 泛型參數(shù)指定的是 Void 就可以不反悔任務(wù)執(zhí)行結(jié)果
     * 注意:該方法中是不可以進(jìn)行 UI 操作的,如果需要更新 UI
     * 元素,比如反饋當(dāng)前任務(wù)的執(zhí)行進(jìn)度,可以調(diào)用 publishProgress 方法來完成
     */
    @Override
    protected Boolean doInBackground(Void... integers) {
        return null;
    }

    /**
     * 當(dāng)在后臺(tái)任務(wù)重調(diào)用了 publishProgress 方法后,該方法就會(huì)很快被
     * 調(diào)用,該方法中攜帶的參數(shù)就是在后臺(tái)任務(wù)重傳遞過來的。在該方法中
     * 可以對(duì) UI 進(jìn)行操作。
     */
    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
    }

    /**
     * 當(dāng)后臺(tái)任務(wù)執(zhí)行完畢并通過 return 語句進(jìn)行返回時(shí),該方法就會(huì)很快調(diào)用,
     * 返回的數(shù)據(jù)會(huì)作為參數(shù)傳遞到此方法中,可以利用返回的數(shù)據(jù)來進(jìn)行一些 UI 操作,
     * 比如提醒任務(wù)執(zhí)行的結(jié)果,以及關(guān)閉進(jìn)度條對(duì)話框等。
     */
    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
    }
}
new DownloadTask().execute();

整體來說,就是在 doInBackground()方法中執(zhí)行具體的耗時(shí)任務(wù),在 onProgressUpdate()方法中進(jìn)行 UI 操作,在 onPostExecute()方法中執(zhí)行一些任務(wù)的收尾工作。

五、服務(wù)

image.png
public class MyService extends Service {
    public MyService() {
    }

    /**
     * 該方法是 Service 中唯一的一個(gè)抽象方法,必須要在子類中實(shí)現(xiàn)
     */
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    /**
     * 創(chuàng)建服務(wù)的時(shí)候調(diào)用
     */
    @Override
    public void onCreate() {
        super.onCreate();
    }

    /**
     * 每次服務(wù)啟動(dòng)的時(shí)候調(diào)用
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 服務(wù)銷毀的時(shí)候調(diào)用
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
        <service
            android:name=".MyService"
            android:enabled="true"
            android:exported="true">
            
        </service>

啟動(dòng)和停止服務(wù)

        findViewById(R.id.tv_start_service).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                    startService 定義在 Context 類中
                 */
                Intent intent = new Intent(FifthActivity.this,MyService.class);
                startService(intent);
            }
        });

        findViewById(R.id.tv_stop_service).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                /*
                    stopService 定義在 Context 類中
                 */
                Intent intent = new Intent(FifthActivity.this,MyService.class);
                stopService(intent);
            }
        });

注意,這里完全是由活動(dòng)來決定服務(wù)何時(shí)停止的,如果沒有點(diǎn)擊 Stop Service按鈕,服務(wù)就會(huì)一直處于運(yùn)行狀態(tài),如果讓服務(wù)自己停下來,可以在 MyService 的任何一個(gè)位置調(diào)用 stopSelf()方法就行了。

活動(dòng)和服務(wù)進(jìn)行通信

??如何讓活動(dòng)和服務(wù)進(jìn)行通信呢?例如在活動(dòng)中指揮服務(wù)去干什么,服務(wù)就去干什么,這就叫借助 onBind()方法了。

public class MyService extends Service {

    private static final String TAG = "MyService";

    private DownloadBinder mBinder = new DownloadBinder();

    /**
     * 新建一個(gè)類繼承自 Binder
     */
    class DownloadBinder extends Binder{

        public void startDownload(){
            System.out.println("abc : startDownload");
        }

        public void getProgress(){
            System.out.println("abc : getProgress");
        }
    }

    public MyService() {
    }

    /**
     * 該方法是 Service 中唯一的一個(gè)抽象方法,必須要在子類中實(shí)現(xiàn)
     */
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "abc onBind: ");
        //返回 DownloadBinder 實(shí)例對(duì)象
        return mBinder;
    }

    /**
     * 創(chuàng)建服務(wù)的時(shí)候調(diào)用
     */
    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("abc : onCreate");
    }

    /**
     * 每次服務(wù)啟動(dòng)的時(shí)候調(diào)用
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        System.out.println("abc : onStartCommand");
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 服務(wù)銷毀的時(shí)候調(diào)用
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("abc : onDestroy");
    }
}
    private MyService.DownloadBinder downloadBinder;

    /**
     * 首先要?jiǎng)?chuàng)建 ServiceConnection 匿名類,重寫 onServiceConnected()方法
     * 和 onServiceDisconnected()方法,這兩個(gè)方法分別會(huì)在活動(dòng)和服務(wù)成功綁定
     * 以及解除綁定的時(shí)候調(diào)用
     */
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            //向下轉(zhuǎn)型獲取 DownloadBinder 實(shí)例對(duì)象,有了該實(shí)例對(duì)象,我們就可以調(diào)用相應(yīng)的方法了
            downloadBinder = (MyService.DownloadBinder) iBinder;
            downloadBinder.startDownload();
            downloadBinder.getProgress();
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };
        findViewById(R.id.tv_bind_service).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FifthActivity.this,MyService.class);
                /*
                    該方法接受三個(gè)參數(shù)
                        參1:Intent
                        參2:ServiceConnection
                        參3:BIND_AUTO_CREATE,表示在活動(dòng)和服務(wù)進(jìn)行綁定后自動(dòng)創(chuàng)建服務(wù)
                            這就會(huì)使onCreate()方法得到執(zhí)行,onStartCommand()方法不會(huì)執(zhí)行。
                 */
                bindService(intent,connection,BIND_AUTO_CREATE);//綁定服務(wù)
            }
        });

        findViewById(R.id.tv_unbind_service).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                unbindService(connection);//解綁服務(wù)
            }
        });
image.png

注意:任何一個(gè)服務(wù)在整個(gè)應(yīng)用程序范圍內(nèi)都是通用的,可以和任何一個(gè)其他的活動(dòng)進(jìn)行綁定,而且在綁定完成后他們都可以獲取到相同的 DownloadBinder 實(shí)例對(duì)象

服務(wù)的生命周期

??只要調(diào)用了 Context 的 startService()方法,相應(yīng)的服務(wù)就會(huì)啟動(dòng)起來,并回調(diào) onStartConmmand()方法,如果這個(gè)服務(wù)之前還沒有創(chuàng)建過,onCreate()方法會(huì)先于 onStartCommand()方法執(zhí)行。服務(wù)一旦啟動(dòng)之后,會(huì)一直保持運(yùn)行狀態(tài),直到 stopService()或stopSelf()方法被調(diào)用。注意,雖然每調(diào)用一次 startService()方法,onStartCommand()就會(huì)執(zhí)行一次,但實(shí)際上每個(gè)服務(wù)都只會(huì)存在一個(gè)實(shí)例,所以不管你調(diào)用多少次 startService(),只需調(diào)用一次 stopService()或stopSelf()方法,服務(wù)就會(huì)停下來。
??另外,還可以調(diào)用 Context 的 bindService()來獲取一個(gè)服務(wù)的持久連接,這時(shí)就會(huì)回調(diào)服務(wù)中的 onBind()方法。類似的,如果服務(wù)之前還沒有創(chuàng)建過,onCreate()方法會(huì)先于 onBind()方法執(zhí)行。然后,調(diào)用方可以獲取到 onBind()方法里返回的 IBinder 對(duì)象實(shí)例。只要調(diào)用方和服務(wù)之間的連接沒有斷開,服務(wù)就會(huì)一直保持運(yùn)行狀態(tài)。
??當(dāng)調(diào)用了 startService()方法后,又去調(diào)用 stopService()方法,這時(shí)服務(wù)中 onDestroy()方法就會(huì)執(zhí)行,表示服務(wù)已經(jīng)銷毀了。類似的,當(dāng)調(diào)用了 bindService()方法后,又去調(diào)用 unbindService()方法,onDestroy()方法也會(huì)執(zhí)行。但是,注意,我們可能一個(gè)服務(wù)既調(diào)用了 startService()又調(diào)用了 bindService()方法,那么該如何銷毀服務(wù)呢?根據(jù) Android 系統(tǒng)的機(jī)制,一個(gè)服務(wù)只要被啟動(dòng)或者被綁定了之后,就會(huì)一直處于運(yùn)行狀態(tài),必須要讓以上兩種條件同時(shí)不滿足,服務(wù)才能被銷毀,所以,這種情況下要同時(shí)調(diào)用 stopService()和 unbindService()方法,onDetroy()方法才會(huì)執(zhí)行。

六、服務(wù)的更多技巧

前臺(tái)服務(wù)

??服務(wù)的系統(tǒng)優(yōu)先級(jí)并不高,當(dāng)系統(tǒng)出現(xiàn)內(nèi)存不足的情況時(shí),就有可能會(huì)回收掉正在后臺(tái)運(yùn)行的服務(wù)。如果你希望服務(wù)可以一直保持運(yùn)行狀態(tài),而不會(huì)由于系統(tǒng)內(nèi)存不足的原因?qū)е卤幌到y(tǒng)回收,就可以考慮使用前臺(tái)服務(wù)。前臺(tái)服務(wù)和普通服務(wù)最大的區(qū)別就在于,前臺(tái)服務(wù)會(huì)一直有一個(gè)正在運(yùn)行的圖標(biāo)在系統(tǒng)的狀態(tài)欄顯示,下拉狀態(tài)欄后可以看到更加詳細(xì)的信息,非常類似于通知的效果。

    public class MyService extends Service {

    private static final String TAG = "MyService";

    public MyService() {
    }

    /**
     * 該方法是 Service 中唯一的一個(gè)抽象方法,必須要在子類中實(shí)現(xiàn)
     */
    @Override
    public IBinder onBind(Intent intent) {
        Log.d(TAG, "abc onBind: ");
        //返回 DownloadBinder 實(shí)例對(duì)象
        return mBinder;
    }

    /**
     * 創(chuàng)建服務(wù)的時(shí)候調(diào)用
     */
    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("abc : onCreate");
        //創(chuàng)建 Intent
        Intent intent = new Intent(this,FifthActivity.class);
        //創(chuàng)建 PendingIntent
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
        //創(chuàng)建 Notification
        Notification notification = new NotificationCompat.Builder(this)
                .setContentText("this is text")
                .setContentTitle("this is title")
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent)
                .build();
        /*
            接收兩個(gè)參數(shù):
                參1:通知的 id
                參2:Notification 對(duì)象
         */
        startForeground(1,notification);
    }

    //...
}

使用 IntentService

??如果使用傳統(tǒng)的 Service,如果要執(zhí)行耗時(shí)操作,需要自己手動(dòng)創(chuàng)建線程,而且在執(zhí)行完畢要記得調(diào)動(dòng) stopSelf()方法關(guān)閉。為了可以簡單地創(chuàng)建一個(gè)異步的、會(huì)自動(dòng)停止的服務(wù),Android 專門提供了一個(gè) IntentService 類。

public class MyIntentService extends IntentService {

    /**
     * 自己改成無參構(gòu)造函數(shù),并且必須在內(nèi)部調(diào)用父類的
     * 有參構(gòu)造函數(shù)
     */
    public MyIntentService() {
        super("MyIntentService");
    }

    /**
     * 該方法是運(yùn)行在子線程當(dāng)中
     */
    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        System.out.println("abc : onHandleIntent");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("abc onDestroy");
    }
}
        findViewById(R.id.tv_intent_service).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FifthActivity.this,MyIntentService.class);
                startService(intent);
            }
        });

七、多線程斷點(diǎn)下載

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

    private static final int TYPE_SUCCESS = 0;
    private static final int TYPE_FAILED = 1;
    private static final int TYPE_PAUSED = 2;
    private static final int TYPE_CANCELED = 3;

    private DownloadListener downloadListener;

    private boolean isCanceled = false;
    private boolean isPaused = false;
    private int lastProgress;

    public DownloadTask(DownloadListener downloadListener) {
        this.downloadListener = downloadListener;
    }

    @Override
    protected Integer doInBackground(String... strings) {
        InputStream is = null;
        RandomAccessFile savedFile = null;
        File file = null;
        try {
            long downloadedLength = 0;//記錄已下載的文件長度
            /*
                獲取下載的 URL 地址,并根據(jù) URL 地址解析出下載的文件名,
                然后指定將文件下載到 Environment.DIRECTORY_DOWNLOADS 目錄下,
                也就是 SD 卡的 Download 目錄
             */
            String downloadUrl = strings[0];
            String fileName = downloadUrl.substring(downloadUrl.lastIndexOf("/"));
            String directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
                    .getParent();
            file = new File(directory + fileName);
            /*
                判斷該目錄下是否已經(jīng)存在要下載的文件,如果存在就獲取該文件的字節(jié)數(shù),
                便于在后面啟動(dòng)斷點(diǎn)續(xù)傳的功能
             */
            if (file.exists()) {
                downloadedLength = file.length();
            }
            long contentLength = getContentLength(downloadUrl);
            if (contentLength == 0) {//文件長度為0,說明文件有問題,下載失敗
                return TYPE_FAILED;
            } else if (contentLength == downloadedLength) {//文件長度等于已下載的文件長度,則下載完成
                return TYPE_SUCCESS;
            }
            //從網(wǎng)絡(luò)下載文件,通過流的方式寫入到本地
            OkHttpClient okHttpClient = new OkHttpClient();
            Request request = new Request.Builder()
                    //斷點(diǎn)下載,指定從哪個(gè)字節(jié)開始下載
                    .addHeader("RANGE", "bytes=" + downloadedLength + "-")
                    .url(downloadUrl)
                    .build();
            Response response = okHttpClient.newCall(request).execute();
            if (response != null) {
                is = response.body().byteStream();
                savedFile = new RandomAccessFile(file, "rw");
                byte[] b = new byte[1024];
                int total = 0;
                int len;
                while ((len = is.read(b)) != -1) {
                    /*
                        判斷用戶有沒有觸發(fā)暫?;蛘呷∠牟僮鳎?                        如果沒有觸發(fā)暫?;蛘呷∠牟僮?,就計(jì)算當(dāng)前的下載進(jìn)度
                     */
                    if (isCanceled) {
                        return TYPE_CANCELED;
                    } else if (isPaused) {
                        return TYPE_PAUSED;
                    } else {
                        total += len;
                    }
                    savedFile.write(b, 0, len);
                    //計(jì)算已下載的百分比
                    int progress = (int) ((total + downloadedLength) * 100 / contentLength);
                    //通知更新界面
                    publishProgress(progress);
                }
                response.body().close();
            }
            return TYPE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
                if(savedFile != null){
                    savedFile.close();
                }
                if(isCanceled && file != null){
                    file.delete();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return TYPE_FAILED;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);
        int progress = values[0];
        if(progress > lastProgress){
            downloadListener.onProgress(progress);
            lastProgress = progress;
        }
    }

    /**
     * 根據(jù)下載完成的狀態(tài)來進(jìn)行回調(diào)
     */
    @Override
    protected void onPostExecute(Integer status) {
        super.onPostExecute(status);
        switch (status){
            case TYPE_SUCCESS:
                downloadListener.onSuccess();
                break;
            case TYPE_FAILED:
                downloadListener.onFailed();
                break;
            case TYPE_PAUSED:
                downloadListener.onPaused();
                break;
            case TYPE_CANCELED:
                downloadListener.onCanceled();
                break;
            default:
                break;
        }
    }

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

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

    /**
     * 獲取下載文件的總長度
     */
    private long getContentLength(String downloadUrl) throws IOException {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(downloadUrl)
                .build();
        Response response = okHttpClient.newCall(request).execute();
        if(response != null && response.isSuccessful()){
            long contentLength = response.body().contentLength();
            response.close();
            return contentLength;
        }
        return 0;
    }
}
public class DownloadService extends Service {

    private DownloadTask downloadTask;

    private String downloadUrl;

    private DownloadBinder mBinder = new DownloadBinder();

    public DownloadService() {
    }

    class DownloadBinder extends Binder {

        public void startDownload(String url) {
            if (downloadTask == null) {
                downloadUrl = url;
                downloadTask = new DownloadTask(listener);
                downloadTask.execute(downloadUrl);
                startForeground(1, getNotification("Downloading...", 0));
                Toast.makeText(DownloadService.this, "Downloading...", Toast.LENGTH_SHORT).show();
            }
        }

        public void pauseDownload() {
            if (downloadTask != null) {
                downloadTask.pauseDownload();
            }
        }

        public void cancelDownload() {
            if (downloadTask != null) {
                downloadTask.cancelDownload();
            } else {
                if(downloadUrl != null){
                    //取消下載時(shí)需將文件刪除,并關(guān)閉通知
                    String fileName = downloadUrl.substring(downloadUrl.lastIndexOf("/"));
                    String directory = Environment.getExternalStoragePublicDirectory(
                            Environment.DIRECTORY_DOWNLOADS).getPath();
                    File file = new File(directory + fileName);
                    if(file.exists()){
                        file.delete();
                    }
                    getNotificationManager().cancel(1);
                    stopForeground(true);
                    Toast.makeText(DownloadService.this, "Canceled", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

    private DownloadListener listener = new DownloadListener() {
        @Override
        public void onProgress(int progress) {
            getNotificationManager().notify(1, getNotification("Downloading...", progress));
        }

        @Override
        public void onSuccess() {
            downloadTask = null;
            //下載成功時(shí)將前臺(tái)服務(wù)通知關(guān)閉,并創(chuàng)建一個(gè)下載成功的通知
            stopForeground(true);
            getNotificationManager().notify(1, getNotification("Download Success", -1));
            Toast.makeText(DownloadService.this, "下載成功", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailed() {
            downloadTask = null;
            //下載失敗時(shí)將前臺(tái)服務(wù)通知關(guān)閉,并創(chuàng)建一個(gè)下載失敗的通知
            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, "Download Pause", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCanceled() {
            downloadTask = null;
            stopForeground(true);
            Toast.makeText(DownloadService.this, "Download Cancel", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    private NotificationManager getNotificationManager() {
        return (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }

    private Notification getNotification(String title, int progress) {
        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setContentTitle(title)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent)
                .build();
        if (progress >= 0) {
            //當(dāng) progress > 0 或者 = 0 時(shí)才需顯示下載進(jìn)度
            builder.setContentText(progress + "%");
            /*
                參1:通知的最大進(jìn)度
                參2:通知的當(dāng)前進(jìn)度
                參3:是否使用模糊進(jìn)度條
             */
            builder.setProgress(100, progress, false);
        }
        return builder.build();
    }
}
public class DownloadActivity extends AppCompatActivity implements View.OnClickListener{

    private DownloadService.DownloadBinder downloadBinder;

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            downloadBinder = (DownloadService.DownloadBinder) iBinder;
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_download);

        TextView startDownload = (TextView) findViewById(R.id.tv_start_download);
        TextView pauseDownload = (TextView) findViewById(R.id.tv_pause_download);
        TextView cancelDownload = (TextView) findViewById(R.id.tv_cancel_download);

        startDownload.setOnClickListener(this);
        pauseDownload.setOnClickListener(this);
        cancelDownload.setOnClickListener(this);

        Intent intent = new Intent(this,DownloadService.class);
        //啟動(dòng)服務(wù),保證服務(wù)一直在后臺(tái)運(yùn)行
        startService(intent);
        //使活動(dòng)和服務(wù)進(jìn)行交互
        bindService(intent,connection,BIND_AUTO_CREATE);
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
        }
    }

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

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

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //注意:一定要在活動(dòng)銷毀的時(shí)候解綁服務(wù),否則會(huì)造成內(nèi)存泄漏
        unbindService(connection);
    }
}
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

下一篇文章:http://m.itdecent.cn/p/41ca26fb4f10

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容