Android Download Manager to listen to progress design ideas anywhere

Source: Internet
Author: User

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

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.