通常在使用service更新應(yīng)用時最常出現(xiàn)的問題就是Notification進(jìn)度的更新問題、service在什么時間關(guān)閉以及需要我們自己在Service中創(chuàng)建新的線程處理耗時操作,當(dāng)然這種也是可以實現(xiàn)的但是會顯得略微繁瑣
經(jīng)過對比發(fā)現(xiàn)可以使用IntentService已經(jīng)實現(xiàn)了對耗時操作的包裝出來,我們只需要實現(xiàn)IntentService中的onHandleIntent方法就可以在其中進(jìn)行耗時操作的處理,在處理下載問題時發(fā)現(xiàn)在使用intentservice時暫時沒有發(fā)現(xiàn)可以優(yōu)雅的進(jìn)行進(jìn)度回調(diào)的實現(xiàn)方法,所以我這邊使用了本地廣播的形式來進(jìn)行進(jìn)度刷新。
添加了當(dāng)前狀態(tài)判斷,當(dāng)應(yīng)用處于前臺狀態(tài)時直接進(jìn)行安裝,當(dāng)應(yīng)用處于后臺時彈出notification彈窗點擊后安裝,示例如下圖:

先創(chuàng)建廣播
public static class MyBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { switch (intent.getAction()) { case ACTION_TYPE_PREPARE: if (downloadCallback != null) { downloadCallback.onPrepare(); } break; case ACTION_TYPE_PROGRESS: int progress = intent.getIntExtra("progress", 0);// Log.d("progress", "|- " + progress + " -|"); if (downloadCallback != null) { downloadCallback.onProgress(progress); } break; case ACTION_TYPE_COMPLETE: String file_path = intent.getStringExtra("file_path"); if (!TextUtils.isEmpty(file_path)) { File file = new File(file_path); if (file.exists()) { if (downloadCallback != null) { downloadCallback.onComplete(file); } } } break; case ACTION_TYPE_FAIL: String error = intent.getStringExtra("error"); if (downloadCallback != null) { downloadCallback.onFail(error + ""); } break; } }然后在IntentService中初始化本地廣播并發(fā)送信息
@Override public void onCreate() { super.onCreate(); mLocalBroadcastManager = LocalBroadcastManager.getInstance(this); } // 在下載進(jìn)度刷新的地方進(jìn)行回調(diào) private void progress(int progress) { Intent intent = new Intent(FileDownloaderManager.ACTION_TYPE_PROGRESS); intent.putExtra("progress", progress); mLocalBroadcastManager.sendBroadcast(intent); } private void downApk(String url) { ..... ..... progress(progress); ..... ..... }在activity中使用
mLocalBroadcastManager = LocalBroadcastManager.getInstance(mContext);mBroadcastReceiver = new MyBroadcastReceiver();IntentFilter intentFilter = new IntentFilter();intentFilter.addAction(ACTION_TYPE_PREPARE);intentFilter.addAction(ACTION_TYPE_PROGRESS);intentFilter.addAction(ACTION_TYPE_COMPLETE);intentFilter.addAction(ACTION_TYPE_FAIL);mLocalBroadcastManager.registerReceiver(mBroadcastReceiver, intentFilter);// ondestory時調(diào)用mLocalBroadcastManager.unregisterReceiver(mBroadcastReceiver);
以上源碼已進(jìn)行封裝,方便使用具體操作步驟如下:
|- 初始化及注冊回調(diào)
//初始化文件下載管理類FileDownloaderManager.init(context)// 注冊下載進(jìn)度監(jiān)聽,并開啟廣播接收FileDownloaderManager.registerDownload(object : FileDownloaderManager.DownloadCallback { override fun onComplete(file: File) = mainView.downloadSucc(file) override fun onFail(msg: String?) = Unit override fun onProgress(progress: Int) = mainView.onProgress(progress) override fun onPrepare() = Unit })//開始下載FileDownloaderManager.download(url)|- 在下載完成后進(jìn)行資源重置
FileDownloaderManager.unbinder()
源碼地址:源碼地址
文檔地址:文檔地址
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。
新聞熱點
疑難解答
圖片精選