我在Firebase中上传/下载文件时使用NotificationCompat.Builder
。上传时是正常的。但是,下载时系统会滞后,直到下载完成并显示日志猫
应用程序可能在其主线程上做了太多工作
这是addOnprogresslistner()
代码
public void onProgress(FileDownloadTask.TaskSnapshot taskSnapshot) {
double fprogress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
String progress = String.format("%.2f", fprogress);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(android.R.drawable.stat_sys_download)
.setContentTitle("Downloading " + model.getName())
.setContentText(" " + progress + "% completed" );
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, mBuilder.build());
}
如果我删除NotificationCompat.Builder
,它工作得很好。
有什么解决方法吗?
您的onPregress
方法经常被调用为上传和下载进度。
您只需要添加一个条件,请检查以下代码:
public void onProgress(FileDownloadTask.TaskSnapshot taskSnapshot) {
double fprogress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
int bytes = taskSnapshot.getBytesTransferred();
String progress = String.format("%.2f", fprogress);
int constant = 1000;
if(bytes%constant == 0)
{
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(android.R.drawable.stat_sys_download)
.setContentTitle("Downloading " + model.getName())
.setContentText(" " + progress + "% completed" );
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, mBuilder.build());
}
}
我希望这会有所帮助。