Android Download Manager monitors progress anywhere

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.