2016-07-13 31 views
1

我在Firebase中上传/下载文件时使用NotificationCompat.Builder。上传时很正常。但是,当下载系统时,直到下载完成并且log cat显示“应用程序可能在其主线程上做了太多工作”。使用Firebase存储下载文件时 - 应用程序可能在其主线程上做了太多工作

这是.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,它工作得很好。

任何解决方法?

+0

'onProgress()'多久调用一次? – Jon

+0

注意,不要忘记你也可以显示一个进度条https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setProgress(int,%20int,%20boolean ) – Jon

回答

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

} 

我希望这会有所帮助。

+0

这只会在0或100%时更新进度,您可能想尝试fprogress%10 == 0或其他什么? – zgc7009

+0

@ zgc7009是的,它只是一个演示,只需将100更改为任何其他常量。我编辑了答案。 – chandil03

+0

@ zgc7009他在这里使用的字节数不是百分比,所以可能是他需要使100到1000或更多。 – chandil03

相关问题