[英]Android- download file + status bar notification slowing down phone
我目前有一個asynctask
從服務器下載MP3。 當用戶開始下載時,會創建狀態欄通知。 這將實時顯示下載進度。 我唯一擔心的是手機放慢了幾乎。 有沒有辦法延遲顯示的進度或使我的代碼更快的方法? 謝謝。
代碼如下:
public class DownloadFile extends AsyncTask<String, String, String> {
CharSequence contentText;
Context context;
CharSequence contentTitle;
PendingIntent contentIntent;
int HELLO_ID = 1;
long time;
int icon;
CharSequence tickerText;
File file;
public void downloadNotification() {
String ns = Context.NOTIFICATION_SERVICE;
notificationManager = (NotificationManager) getSystemService(ns);
icon = R.drawable.sdricontest;
//the text that appears first on the status bar
tickerText = "Downloading...";
time = System.currentTimeMillis();
notification = new Notification(icon, tickerText, time);
context = getApplicationContext();
//the bold font
contentTitle = "Your download is in progress";
//the text that needs to change
contentText = "0% complete";
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setType("audio/*");
contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(HELLO_ID, notification);
}
@Override
protected void onPreExecute() {
//execute the status bar notification
downloadNotification();
super.onPreExecute();
}
@Override
protected String doInBackground(String... url) {
int count;
try {
URL url2 = new URL(sdrUrl);
HttpURLConnection connection = (HttpURLConnection) url2.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.connect();
int lengthOfFile = connection.getContentLength();
//make the stop drop rave folder
File sdrFolder = new File(Environment.getExternalStorageDirectory() + "/StopDropRave");
boolean success = false;
if (!sdrFolder.exists()) {
success = sdrFolder.mkdir();
}
if (!success) {
String PATH = Environment.getExternalStorageDirectory()
+ "/StopDropRave/";
file = new File(PATH);
file.mkdirs();
} else {
String PATH = Environment.getExternalStorageDirectory()
+ "/StopDropRave/";
file = new File(PATH);
file.mkdirs();
}
String[] path = url2.getPath().split("/");
String mp3 = path[path.length - 1];
String mp31 = mp3.replace("%20", " ");
String sdrMp3 = mp31.replace("%28", "(");
String sdrMp31 = sdrMp3.replace("%29", ")");
String sdrMp32 = sdrMp31.replace("%27", "'");
File outputFile = new File(file, sdrMp32);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream input = connection.getInputStream();
byte[] data = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress("" + (int) (total * 100 / lengthOfFile));
fos.write(data, 0, count);
}
fos.close();
input.close();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
public void onProgressUpdate(String... progress) {
contentText = Integer.parseInt(progress[0]) + "% complete";
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(HELLO_ID, notification);
super.onProgressUpdate(progress);
}
}
我有過類似的問題,我使用CountDownTimer解決了它。
與@superfell建議的類似,您可以在下載文件時定期調用AsyncTask的進度更新。 並且只在特定時間間隔調用通知管理器。
在調用CountDownTimer的start()
之后,它將在每個固定的時間間隔之后調用onTick()
函數,並在計時器超時或顯式調用時調用onFinish()
。 cancel()
函數只會取消定時器而不會調用onFinish()
方法。
class DownloadMaterial extends AsyncTask<String, String, String> {
CountDownTimer cdt;
int id = i;
NotificationManager mNotifyManager;
NotificationCompat.Builder mBuilder;
@Override
protected void onPreExecute() {
/**
* Create custom Count Down Timer
*/
cdt = new CountDownTimer(100 * 60 * 1000, 500) {
public void onTick(long millisUntilFinished) {
mNotifyManager.notify(id, mBuilder.build());
}
public void onFinish() {
mNotifyManager.notify(id, mBuilder.build());
}
};
}
@Override
protected String doInBackground(String... strings) {
/**
* Start timer to update Notification
* Set Progress to 20 after connection
* Build Notification
* Increment Progress
* Download and Save file
*/
try {
mNotifyManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(context);
mBuilder.setContentTitle("Downloading File")
.setContentText(file_name)
.setProgress(0, 100, false)
.setOngoing(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(Notification.PRIORITY_LOW);
// Initialize Objects here
publishProgress("5");
mNotifyManager.notify(id, mBuilder.build());
cdt.start();
// Create connection here
publishProgress("20");
// Download file here
while ((count = input.read(data)) != -1) {
total += count;
publishProgress("" + (int) (20 + (total * 80 / fileLength)));
output.write(data, 0, count);
}
} catch (Exception e) {
return "Failed";
}
return "Success";
}
@Override
protected void onProgressUpdate(String... values) {
/**
* Update Download Progress
*/
mBuilder.setContentInfo(values[0] + "%")
.setProgress(100, Integer.parseInt(values[0]), false);
}
@Override
protected void onPostExecute(String s) {
String title;
if (s.equals("Success")) {
title = "Downloaded";
} else {
title = "Error Occurred";
}
mBuilder.setContentTitle(title)
.setContentInfo("")
.setOngoing(false)
.setProgress(0, 0, false);
cdt.onFinish();
cdt.cancel();
}
}
首先調用onFinish()
然后調用cancel()
是一個好習慣。
我看到類似的結果,你需要不經常推送更新通知,我改變我的更新只更新幾次一秒鍾。 (例如,在onProgressUpdate中跟蹤您最后一次調用notify,並且只有在您超過前一次調用的100ms時,或者如果您處於最大值時才調用notify。
我也遇到過這個問題。 我經常更新進度條WAY (即使進度沒有改變),這是我如何修復:
// While loop from generic download method.
int previousProgress = 0;
while ((count = inputStream.read(buff)) != -1) {
outputStream.write(buff, 0, count);
totalBytesDownloaded += count;
int prog = (int) (totalBytesDownloaded * 100 / contentLength);
if (prog > previousProgress) {
// Only post progress event if we've made progress.
previousProgress = prog;
myPostProgressMethod(prog);
}
}
現在應用程序運行良好,用戶仍然收到進度通知。
我有同樣的問題,即使間隔3秒我也無法更新進度條通知,所以經過幾個小時的挖掘后我才意識到每當我們更新通知時,必須重新實例化RemoteView對象並重新啟動 - 初始化為Notification對象的contentView。 執行此操作后,我能夠在很長一段時間內以100ms-500ms的間隔更新通知進度條,而不會遇到任何UI阻塞。
注意:如果您不同意,可以在注釋掉標記的行並查看差異后運行此代碼段來驗證此答案。 可能需要大約5分鍾才能啟動嚴重的UI阻塞,這會阻塞您的設備並可能停止運行。 我嘗試使用Android 4.2.2的S3 mini,並且從服務中的工作線程調用updateNotification(....)方法。 而且我已經仔細檢查了它,並且不知道當Notification.Builder用於同一目的時會發生什么。
注意:在問題3年之后寫這個答案的原因是因為我想我甚至找不到一個stackoverflow答案或其他博客文章處理這個非常簡單的解決方案的嚴重問題。
我希望這個答案對我這樣的其他新手有所幫助。 請享用。
這是我可以直接使用的復制粘貼代碼....我使用相同的代碼更新通知布局,其中包含兩個ProgressBars和四個TextViews,頻率為500ms-100ms。
//long mMaxtTimeoutNanos = 1000000000 // 1000ms.
long mMinTimeNanos = 100000000;//100ms minimum update limit. For fast downloads.
long mMaxtTimeoutNanos = 500000000;//500ms maximum update limit. For Slow downloads
long mLastTimeNanos = 0;
private void updateNotification(.....){
// Max Limit
if (mUpdateNotification || ((System.nanoTime()-mLastTimeNanos) > mMaxtTimeoutNanos)) {
// Min Limit
if (((System.nanoTime() - mLastTimeNanos) > mMinTimeNanos)) {
mLastTimeNanos = System.nanoTime();
// instantiate new RemoteViews object.
// (comment out this line and instantiate somewhere
// to verify that the above told answer is true)
mRemoteView = new RemoteViews(getPackageName(),
R.layout.downloader_notification_layout);
// Upate mRemoteView with changed data
...
...
// Initialize the already existing Notification contentView
// object with newly instatiated mRemoteView.
mNotification.contentView = mRemoteView;
mNotificationManager.notify(mNotificatoinId, mNotification);
mUpdateNotification = false;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.