简体   繁体   中英

How to download using notification icon and status

In my java android application I can download files served by a web service - I've developed the download "manually" - reading byte by byte - but I'm wondering how can I do to when downloading, appear that icon on notification bar (similar when downloading from market) - also would be nice do download asynchronously, something that I don't do now - I suppose I have to use Async Task but I have no idea how to use it.

About the notification status, I'm supposing that I have to use some function from the sdk - I've found the download manager class in android references however it's only available since api level 9.

EDIT: Just found what part of what I need here , however I really appreciate if anyone can explain how can I adapt that do a notification icon with status.

UPDATE

I already have the async task working when downloading files. In my class I have this code:

while ((count = input.read(data)) != -1) {
                total += count;
                conta = (int)(total*100/lenghtOfFile); //calculate the received data
                publishProgress(conta); // this does nothing
                output.write(data, 0, count); // write file received
                Log.v("conta:", Integer.toString(conta)); // it's right - 0 -100%
            }

The variable "conta" it's correct, it goes from 0 to 100%. My problem is showing that information to the user. I would like that information appears on the notification bar showing a progress bar on it. - something like market downloads.

I found a good example here :

The problem, as I use async task:

public class DownloadFile extends AsyncTask<String, Integer, String>{
    @Override
    protected String doInBackground(String... urlpassado) {

I cannot do changes in the layout, I have problems like

The method setContentView(int) is undefined for the type DownloadFile

How can I solve this?

They have a fairly decent tutorial here

If you are asking something else let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM