Downloadmanager in android
Downloadmanager使用
DownloadManager是android提供的一個下載管理器
缺點:不支援斷點續傳
使用方法:
建立例項:
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
建立下載任務:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(URL)); //指定儲存地址 request.setDestinationInExternalPublicDir("chuyi", "meizhi.apk"); //設定允許下載的網路狀況 request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); //設定通知欄的行為 request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //通過id唯一標識此下載任務 long id = manager.enqueue(request);
刪除下載任務:
manager.remove(id);
查詢下載任務:
DownloadManager.Query query = new DownloadManager.Query(); query.setFilterById(id); Cursor cursor = manager.query(query); if(cursor.moveToFirst()){ String filename = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME)); String fileUri = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); Toast.makeText(context,"下載完成...name:"+filename+",uri:"+fileUri,Toast.LENGTH_SHORT).show(); }else { //TODO } cursor.close();
查詢下載進度:
DownloadManager.Query query = new DownloadManager.Query().setFilterById(downloadId); Cursor c = null; try { c = downloadManager.query(query); if (c != null && c.moveToFirst()) { int downloadedBytes = c.getInt(c.getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); int totalBytes = c.getInt(c.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); int state = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)); } } finally { if (c != null) { c.close(); } }
監聽下載結束通知:
可以通過接收DownloadManager.ACTION_DOWNLOAD_COMPLETE廣播來監聽下載結束的通知
IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE); mReceiver = new BroadcastReceiver(){ public void onReceive(Context c,Intent i){ long downId = i.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,-1); } } context.registerReceiver(mReceiver,filter);
監聽下載進度:
主要有三種方案:1.FileReceiver 2.ContentObserver 3.定時任務
class DownloadChangeObserver extends ContentObserver { public DownloadChangeObserver(){ super(handler); } @Override public void onChange(boolean selfChange) { //查詢進度 } } //in activity private DownloadChangeObserver downloadObserver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.download_manager_demo); …… downloadObserver = new DownloadChangeObserver(); } @Override protected void onResume() { super.onResume(); /** observer download change **/ getContentResolver().registerContentObserver(DownloadManagerPro.CONTENT_URI, true, downloadObserver); } @Override protected void onPause() { super.onPause(); getContentResolver().unregisterContentObserver(downloadObserver); }
上面這種做法可能對效能有些損耗,因為會不斷觸發onChange
推薦使用ScheduledExecutorService
public static ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(3); Runnable command = new Runnable() { @Override public void run() { updateView(); } }; scheduledExecutorService.scheduleAtFixedRate(command, 0, 3, TimeUnit.SECONDS);
參考資料:
相關文章
- Android 中 DownLoadManager 實現檔案下載Android
- Android 使用 DownloadManager 管理系統下載任務的方法Android
- Android開發之自帶下載器DownloadManager的使用Android
- Android學習—— Android佈局Android
- Android之android exported="false"作用AndroidExportFalse
- [Android元件化]Android app BundleAndroid元件化APP
- 聚焦 Android 11: Android 開發者工具Android
- 【Android】Android設計準則Android
- Android Transition(Android過渡動畫)Android動畫
- 愛上Android之初識AndroidAndroid
- [Android]Android原始碼下載Android原始碼
- [android]android命令列截圖Android命令列
- 迴歸Android Focus on Android.Android
- AndroidAndroid
- android:id="@android:id/tabhost" 、android:id="@+id/llRoot" 、android:id="@id/llRoot" 之間的區別Android
- android開發 之 Bolts-AndroidAndroid
- Android總結篇系列:Android ServiceAndroid
- Android APIs (Class Index - Android SDK)(一)AndroidAPIIndex
- Android APIs (Class Index - Android SDK)(二)AndroidAPIIndex
- Android元件---重新認識Android(2)Android元件
- 【Android】Android 簽名相關問題Android
- Android 面試(五):探索 Android 的 HandlerAndroid面試
- 【Android】神奇的android:clipChildren屬性Android
- 【Android】Android輸入子系統Android
- [Android]calabash-android再深入Android
- android之 Android訊息機制Android
- [android]android自動化測試Android
- [android]android-am命令的使用Android
- [Android]android自動化測試十六之calabash-androidAndroid
- Android Wear-Detecting Location on Android Wear,Requesting Permissions on Android WearAndroid
- [Android Pro] android 4.4 Android原生許可權管理:AppOpsAndroidAPP
- android之android:paddingLeft和android:layout_marginLeft區別: 分類: Android android開發實踐 Android入門教程 2011-0...Androidpadding
- 【Android SDK】在命令列管理Android SDKAndroid命令列
- Android之Service設定android:process作用Android
- Android Studio 2.0 to Android Studio 3.0Android
- Android非同步----重新認識Android(6)Android非同步
- Android屬性設定android:noHistory="true"Android
- Android元件框架:Android檢視片段FragmentAndroid元件框架Fragment