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