最近項目中要求使用httpUrlConnection實現(xiàn)多任務(wù)多線程的下載器灭翔,并不能使用三方的類庫。研究各方的下載器:http://www.reibang.com/p/798a99d871a0、
https://blog.csdn.net/weixin_39637545/article/details/117347271 和
http://www.reibang.com/p/80ab95d5ba07。這幾篇都不錯。
自己也整理了一下元莫。下載鏈接: 鏈接: https://pan.baidu.com/s/1JqyTtsXs9BTGx1-KemH02Q?pwd=h9wf 提取碼: h9wf
在過程中,也遇到了一些通知欄的兼容性問題蝶押。解決參考了幾個位作者的文章踱蠢。
1. android 8.0 通知不顯示問題
https://www.likecs.com/show-203767074.html
是channelId的問題
2. Android 各個版本的兼容性問題
https://www.dandelioncloud.cn/article/details/1514199004341682178
3.Android 11 獲取存儲路徑錯誤 android.system.ErrnoException: open failed: EPERM (Operation not permitted),Android11 不能獲取sdcard通用空間路徑棋电。
private String getDestPath() {
String path;
if (Build.VERSION.SDK_INT > 29) {
path = mContext.getExternalFilesDir(null).getAbsolutePath() + "/log/update/";
} else {
path = Environment.getExternalStorageDirectory().getPath() + "/log/update/";
}
return path;
}
獲取的空間路徑為 Android/data/XXXX(應(yīng)用包名)/log
4.系統(tǒng)UI通知欄增加按鈕和刪除按鈕
https://www.it1352.com/2316980.html
notifBuilder.mActions.clear();
https://blog.csdn.net/androidwubo/article/details/76021402
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_notification)
.setContentTitle(getString(R.string.notification))
.setContentText(getString(R.string.ping))
.setDefaults(Notification.DEFAULT_ALL)
// 該方法在Android 4.1之前會被忽略
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
//添加Action Button
.addAction (R.drawable.ic_stat_dismiss,
getString(R.string.dismiss), piDismiss)
.addAction (R.drawable.ic_stat_snooze,
getString(R.string.snooze), piSnooze)
注意addAction方法建議與setContentIntent一起使用茎截。
5. 在自研下載器前,也考慮用系統(tǒng)的下載器赶盔。但是系統(tǒng)的下載器有很多問題企锌,沒辦法支持很多操作。
5.1)簡單的使用
https://blog.csdn.net/qq_35560164/article/details/119485310
5.2)0k長度的文件在下載時不能回調(diào)于未,一直下載中https://blog.csdn.net/lz8362/article/details/78767508?spm=1001.2014.3001.5501
5.3)刷新UI撕攒, 沒有正在下載中的回調(diào)事件,需要啟動定時器不斷從數(shù)據(jù)庫讀取數(shù)據(jù)
http://www.reibang.com/p/d2ab2449dd12
5.4)Android 7.0 的讀取數(shù)據(jù)庫時字段的變更
https://www.wingmei.cn/2018/02/05/android-7-0無法獲取downloadmanager下載進度/
5.5)啟動安裝apk時uri 報錯android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW type=application/vnd.android.package-archive flg=0x10000000 }
https://blog.csdn.net/fengyeNom1/article/details/73868577