2
我有一个下载任务,定期向通知报告进度。一段时间以来,我每次都使用一个RemoveView私有成员进行更新。有人可以解释RemoteViews GC行为?
例如:
private RemoteViews mRemoteView;
protected void onCreate(){
mRemoteView = new RemoteViews(getPackageName(), R.layout.custom_layout)
contentView.setImageViewResource(R.id.notification_icon, R.drawable.downloads);
contentView.setTextViewText(R.id.notification_text, "Downloading A File " + (int)((double)progress/(double)max * 100) + "%");
contentView.setProgressBar(R.id.mProgress, max, progress, false);
notification.contentView = contentView;
mNotificationManager.notify(HELLO_ID, notification);
}
protected void onProgressUpdate(Integer... prog) {
contentView.setProgressBar(R.id.mProgress, max, progress, false);
mNotificationManager.notify(HELLO_ID, notification);
}
然而,我发现,GC不断清理出空间,拖慢该应用程序来抓取很长一段时间。我试着每次更新时创建一个新的RemoteViews,并且工作正常。我想知道这是为什么。我找到了一个有用的链接here,但我在寻找更多信息。
下面是工作代码:
protected void onProgressUpdate(Integer... prog) {
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
contentView.setImageViewResource(R.id.notification_icon, R.drawable.downloads);
contentView.setTextViewText(R.id.notification_text, "Downloading A File " + (int)((double)progress/(double)max * 100) + "%");
contentView.setProgressBar(R.id.mProgress, max, progress, false);
notification.contentView = contentView;
mNotificationManager.notify(HELLO_ID, notification);
}
感谢您的回答。随着时间的推移,队列会被删除,但正确吗?因此,如果我的onProgressUpdate()每1小时被调用一次(仅使用一个很长的示例),那么应该没有问题?在我的例子中,它更新得非常频繁,所以我可以看到为什么这个队列会成为问题。 – Pzanno 2011-02-23 23:42:17
是的,使用速度慢,问题不会那么严重 - GC能够应付这项工作。但是这并不能改变RemoteViews中较新的队列被清除的事实。 RemoteViews只是一组构建View的命令。如果您不断在其上添加命令而不是在远程端创建视图,则需要更长的时间。 – 2011-02-24 09:07:48