Download a file, for example, Android Market

I am going to implement a function that will download a large file (about 50 MB) in the background.

I would like to do this as the Android Market - I mean, when the user starts downloading the file, he will appear in the status bar with all this progress bar and notify my application when it is completed.

Could you give me some advice? I know that my question is not high quality, but I have been doing research before, and I just do not have keywords to find a solution.

+4
source share
2 answers

This is just a short review to give you a few keywords.

First of all, how to create a notification should be fairly simple and well documented. If you do not know how to create a normal notification, check out Status line notification .

The next step is to create a notification with a custom layout containing the ProgressBar (since there is no ready layout for this afaik), which is also documented on the page . Having created an instance of Notification for this, you must save the link and use it to update the ProgressBar through

 notification.contentView.setProgressBar(R.id.yourprogressbar, 100, 42, false); nm.notify(notificationId, notification); 

nm is a NotificationManager link here, also see RemoteViews.setProgressBar ()

This is basically the UI side of things. To download a file in the background, you must use a Service that uses AsyncTask (since services run in a UI thread - the name is often misleading). You can use AsyncTask.publishProgress() to send progress updates to the user interface and update the progress bar inside AsyncTask.onProgressUpdate() .

+7
source

In addition to @alextsc's answer, if you only support APIs of level 9 and above, you can use the DownloadManager , which handles all of this for you, including details such as connection changes (e.g. WiFi-> 3G). But, which is only available on Android 2.3+.

+3
source

Source: https://habr.com/ru/post/1381928/


All Articles