Android通知是重建而不是更新? [英] Android notification rebuilds instead of updating?
问题描述
我正在下载文件,我想让用户了解进度,所以我创建了一个通知.问题是通知不断重建而不是更新.我在网上搜索了类似的行为,并且按照他们说的做了,但是我的问题仍然存在.
I'm downloading a file and i want to keep the user informed about the progress so i created a notification.The problem is that the notification keeps getting rebuild instead of updating. I searched for similar behaviours online and i did as they said but my problem persists.
每个人都在说要使用通知生成器,因此它第一次构建整个通知,然后仅更新我告诉它的内容.尝试过,仍然无法正常工作.
Everybody is saying to use a notification builder so the first time it builds the whole notification and then only updates what i tell it to.Tried it,still not working.
这里我要声明通知和NotificationManager.
Here i'm declaring the notification and the notificationManager.
private NotificationCompat.Builder notification;
private NotificationManager notificationManager;
这是创建通知的方法.DOWNLOAD_NOTIFICATION_ID是值为2的最终int
This is the method that creates the notification.DOWNLOAD_NOTIFICATION_ID is a final int with value 2
private void createDownloadNotification(){
notificationManager = (NotificationManager) getSystemService(GuideSelected.this.NOTIFICATION_SERVICE);
notification = new NotificationCompat.Builder(GuideSelected.this,CHANNEL_DOWNLOAD)
.setSmallIcon(android.R.drawable.stat_sys_download) // here is the animated icon
.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), android.R.drawable.stat_sys_download))
.setContentTitle("title")
.setContentText("Download starting...")
.setStyle(new NotificationCompat.BigTextStyle())
//.setContentIntent(pendingIntent)
//la notifica si cancella da sola dopo che clicco
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_LOW)
.setProgress(100,0,false);
notificationManager.notify(DOWNLOAD_NOTIFICATION_ID,notification.build());
}
}
这是我要更新通知的异步任务
This is the asynctask where i'm updating the notification
private class DownloadFile extends AsyncTask<String, String, String> {
final Handler handler = new Handler();
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... f_url) {
int count;
// HERE I'M CREATING THE NOTIFICATION
createDownloadNotification();
//DOWNLOADING STUFF....
}
// Updating progress bar
protected void onProgressUpdate(String... progress) {
// UPDATING IT EVERY 5% SO I DON't MAKE TOO MANY CALLS
currentPercentage=Integer.parseInt(progress[0]);
if (previousPercentage != currentPercentage && currentPercentage%5==0) {// line : 6
notification.setProgress(100,previousPercentage, false).setContentText(previousPercentage+"%").setSubText("Downloading...");
notificationManager.notify(DOWNLOAD_NOTIFICATION_ID, notification.build());
previousPercentage=currentPercentage;
}
}
@Override
protected void onPostExecute(String message) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
notification.setProgress(0,0,false).setOngoing(false).setContentText("Download complete").setSubText(null);
notificationManager.notify(DOWNLOAD_NOTIFICATION_ID, notification.build());
}
}, 2000);
}
}
我只希望通知在每次更新时都无需重建,我可以从动画下载图标中看到它的重建,因为它一直在重新启动.
I just want the notification to update without rebuilding everytime, i can see it rebuilds from the animated download icon because it keeps restarting.
添加了通知和通知管理器的声明.
Added the declaration of notification and notification manager.
推荐答案
In the end i solved it by using custom download intent service class which extends IntentService and when i'm updating the notification i'm controlling if the last % is different than the current one (since the updateProgress is called multiple times in a second) and i also added a timer so it only updates every 1.3 seconds (this is the time that the download icon aniamtion takes to complete) and in this way i finally achieved my goal and the notification doesn't continuously rebuild.
这是我更新通知的方式:
Here is how i'm updating the notification:
while ((count = input.read(data)) != -1) {
if (Constants.stopService) {
output.flush();
output.close();
input.close();
stopSelf();
return;
}
if (count != 0) {
total += count;
latestPercentDone = (int) Math.round(total / lengthOfFile * 100.0);
if (percentDone != latestPercentDone) {
percentDone = latestPercentDone;
if (SystemClock.elapsedRealtime() - mLastClickTime > 1300) { // 1000 = 1second
mLastClickTime = SystemClock.elapsedRealtime();
sendMessage(percentDone, idLesson);
notification.setProgress(100, percentDone, false);
notificationManager.notify(realNotificationID, notification.build());
}
}
output.write(data, 0, count);
}
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
这篇关于Android通知是重建而不是更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!