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

While downloading files using firebase storage -The application may be doing too much work on its main thread

我在 Firebase 中使用 NotificationCompat.Builder 而 uploading/downloading 文件。上传时是正常的。然而,当下载系统滞后,直到下载完成并且 log cat 显示

The application may be doing too much work on its main thread

这是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());
}

}

希望对您有所帮助。