We must be, do the application market, or other downloads, want to listen to the progress of any place, and very good implementation.
It is easy to share a situation here, and prevent memory overflow. We use a weak reference to bind the view to update the progress method.
map<string, weakreference<view>> viewlistem = new hashmap<string, weakreference<view>> ();// Weak reference to a progress bar
Prevent memory from being recycled when it is not enough.
Key is
Value progress bar
Add a progress bar reference.
Public synchronized void Addviewtocache (String path, view view) {
if (Viewlistem = = null) {
Return
}
weakreference<view> Softview = new weakreference<view> (View);
Viewlistem.put (path, softview);
}
Gets the progress bar.
Public synchronized View Getviewbypath (String path) {
if (Viewlistem = = null) {
return null;
}
To take a soft-referenced view object from the cache
weakreference<view> Softview = viewlistem.get (path);
Determine if a soft reference exists
if (Softview = = null) {
return null;
}
Remove the View object, if the view is recycled due to low memory, it will get empty
View view= softview.get ();
return view;
}
The next step is to go to the callback interface of the progress listener and get the corresponding view to update the progress. Get.
is not both simple and easy to achieve. It also prevents memory overflow.
Download Manager on Android to listen to the progress design ideas anywhere