Android Download Manager monitors progress anywhere
You may want to monitor the progress anywhere in the application market or other downloads, and implement it well.
It is relatively simple to share and prevent memory overflow. We use a weak reference to bind the View to update the progress method.
Map > ViewListem = new HashMap > (); // Weak reference to the progress bar
Prevent recovery when the memory is insufficient.
Key is
Value progress bar
// Add progress bar reference.
Public synchronized void addViewToCache (String path, View view ){
If (viewListem = null ){
Return;
}
WeakReference SoftView = new WeakReference (View );
ViewListem. put (path, softView );
}
// Obtain the progress bar.
Public synchronized View getViewByPath (String path ){
If (viewListem = null ){
Return null;
}
// Obtain the soft reference View object from the cache
WeakReference SoftView = viewListem. get (path );
// Determine whether soft reference exists
If (softView = null ){
Return null;
}
// Retrieve the View object. If the View is recycled due to insufficient memory, null is obtained.
View view = softView. get ();
Return view;
}
Next, go to the callback interface of the Progress listener and get the corresponding View to update the progress. Done.
Is it simple and easy to implement. It also prevents memory overflow.