使用firebase存储下载文件时 - 应用程序可能在其主线程上做了太多工作

时间:2016-07-13 14:42:55

标签: android firebase firebase-storage

我在Firebase中上传/下载文件时使用NotificationCompat.Builder。上传时很正常。但是,下载系统时滞后直到下载完成并且日志cat显示

  

应用程序可能在其主线程上做了太多工作

这是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,它就可以了。

任何解决方法?

1 个答案:

答案 0 :(得分:2)

您的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());
}

}

我希望它会有所帮助。