Android intentservice based file download example code

Downloading files is time-consuming. Previously, asynchronous classes such as asynctask were used for downloading, and then they were switched to the background and killed. So I'm going to try service. (however, according to the urine quality of the current system, the service is actually killed every minute)

However, instead of using the service class directly, the intentservice inherited from the service is used.

This thing has three advantages:

1. He has a task queue; 2. The task queue will stop automatically after execution; 3. It will play an independent thread, and the time-consuming operation will not affect the main thread of your app.

Such an automated thing is easy to worry about.

Don't say much, start rolling the code.

First, to build an application, the main file is as follows (the layout code will not be pasted):

The above code doesn't matter, huh.

Next is the point. Create an intentservice class and override its onhandleintent.

The tasks to be performed are written in onhandleintent

Here, first use thread.sleep to simulate a time-consuming task. After a trial run, you can see that the service is still running after the main app is closed, and it will destroy itself after running.

Receive tasks through intent. Here, start the service through startservice in mainactivity

Of course, you have to register in androidmanifest. XML

Next, let's see how to download the file

First, don't forget to add permissions:

Add the downloadfile method to manage downloads.

The download instructions are in the notes.

Put onhandleintent to execute:

Run, um, silently download it.

However, as a responsible app, we certainly need to give feedback to users, so we need to show the progress.

We use notification to display the progress. (it should be noted that if it is necessary to call the main UI thread to display the progress, it should be fully considered that your app may not be active all the time during the operation of the service, and may have been destroyed long ago.) (of course, using binding to start the service is another use scenario.) before downloading (that is, before executing the downloadfile method) Create and set the notification first.

Notificationcompat. Builder () is used here. If you do not consider the compatibility of older versions, you can use the notification. Builder () method.

The notification is then sent to the system using the notificationmanager. Notify () method. If it needs to be updated, notify () again to the notification with the same ID. if the notification already exists, it will be updated. If it does not exist, it will be created.

In order to display the progress, the handler and runnable are used to refresh regularly, and the progress bar is displayed through the setprogress method.

You can use manager. Cancelall() if you need to clear the notification after you finish; Or manager. Cancel (int);

Complete code again

Address of related GitHub project: servicedownloaderdemo

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>