小菜之前在 Android 處理文件下載過程中遇到以下幾個小問題,小菜簡單整理一下;
Download
重定向文件下載如何獲取文件類型?
小菜在下載過程中通常需要獲取文件名稱和文件類型等進行具體的業務處理;而下載類的鏈接也不是固定格式的,主要區分爲 https://github.com/ace.../test.apk 以及 https://github.com/ace...?app=acetest 等經過重定向之後的下載鏈接;
針對第一種類型鏈接,小菜可以方便的獲取文件類型和名稱等一系列信息,針對第二種重定向類型鏈接,小菜嘗試瞭如下幾種方式;
方案一:
小菜嘗試通過 BufferedInputStream 獲取文件類型,其中調用時需要進行異步操作,而結果並不如意,很多文件類型不能直接識別;
private String getFileType(String path) {
String type = "";
BufferedInputStream bufferedInputStream = null;
HttpURLConnection urlconnection = null;
try {
URL url = new URL(path);
urlconnection = (HttpURLConnection) url.openConnection();
urlconnection.connect();
bufferedInputStream = new BufferedInputStream(urlconnection.getInputStream());
type = HttpURLConnection.guessContentTypeFromStream(bufferedInputStream) + "";
} catch (IOException e) {
e.printStackTrace();
}
return type;
}
方案二:
小菜藉助 OKHttp 方式將重定向的 URL 轉爲起始狀態 URL,從而獲取文件名稱,文件類型等;但該方式調用時也需要異步操作,不能在主線程中執行;
private void getRealUrl(String url) {
if (!Empty.check(url)) {
try {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(url).build();
Response response = client.newCall(request).execute();
HttpUrl realUrl = response.request().url();
if (!Empty.check(realUrl) && !Empty.check(realUrl.url())) {
String temp = realUrl.toString();
String fileName = temp.substring(temp.lastIndexOf("/") + 1);
if (fileName.contains("?")) {
fileName = fileName.substring(0, fileName.lastIndexOf("?"));
}
...
}
} catch (IOException e) {
Logger.e(TAG, Empty.check(e) ? "" : e.getMessage());
}
}
}
方案三:
在具體特定 WebView 場合,可以通過 WebView 預先加載之後獲取起始下載鏈接,之後在進行具體的業務邏輯操作;
小菜嘗試了多種方式,對於重定向類型下載鏈接基本都需要異步耗時操作,暫時還未找到更簡單快捷的方式;
Notification
Notification 在日常應用場景非常多,而配合下載類提示用戶時小菜遇到幾個小問題,簡單整理一下;
1. 使用進度條時提示音一直播放?
小菜測試時,使用進度條 setProgress 時,隨着進度的進行提示音一直在提醒,此時可以設置 NotificationCompat.Builder.setOnlyAlertOnce 只提醒一次即可;於此同事,部分手機時間一直在閃動,例如:剛剛 一直在提示,可以通過 setShowWhen 關閉時間戳提示即可;
NotificationCompat.Builder notification =
new NotificationCompat.Builder(context, notifyId + "").setSmallIcon(R.drawable.icon)
.setSound(null)
.setVibrate(null)
.setContentTitle(title)
.setContentText(des)
.setAutoCancel(true)
.setShowWhen(false)
.setOnlyAlertOnce(true)
.setProgress(100, downloadProgress, false);
2. 結束後點擊通知欄消息不消失?
小菜測試在設置點擊自動關閉屬性 setAutoCancel 後,完成下載,點擊通知欄消息時,該 Notification 未消失;其原因在於小菜省略了設置 setContentIntent 的 PendingIntent;即便是不需要跳轉安裝或其他具體頁面,也需要設置一個默認的 PendingIntent;
PendingIntent pendingIntent;
if (fileType != null && fileType.equals(FileDownloadManager.FILE_TYPE_APK)) {
pendingIntent = PendingIntent.getActivity(context, 0, clickIntent, 0);
} else {
pendingIntent = PendingIntent.getBroadcast(context, REQUESTCODE, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
NotificationCompat.Builder notification =
new NotificationCompat.Builder(context, notifyId + "").setSmallIcon(R.drawable.icon)
.setContentText(des)
.setContentIntent(pendingIntent);
3. 如何左右滑動清除通知監聽?
小菜之前未嘗試過滑動清除 Notification,實際與設置點擊通知操作類似,也需要設置對應的 PendingIntent 到 setDeleteIntent 即可;
pendingIntent = PendingIntent.getBroadcast(context, REQUESTCODE, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notification =
new NotificationCompat.Builder(context, notifyId + "").setSmallIcon(R.drawable.icon)
.setContentText(des)
.setDeleteIntent(pendingIntent);
小菜在測試過程中,學習了很多之前不常用的屬性,內容都很簡單,小菜不做具體的介紹;主要是對於重定向文件下載的一個小積累;如有錯誤,請多多指導!
來源: 阿策小和尚