Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ZlsamDownloadService

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how to use ZlsamDownloadService, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Not long ago, when I was doing a download module for the company's smart TV, I found that the DownloadManager included in android did not respond in the case of cable network. I looked at the source code and found that the DownloaderManager of the standard mobile version of android did not deal with the situation of the wired network, so I spent some time writing a download module. Considering that most of the smart TVs in China are based on the mobile version of android, so share it here.

ZlsamDownloadService is an android service that can manage multiple downloads, and you use it as an alternative to DownloadManager. The specific functions are as follows:

Support for multitasking

Thread safety

Support for wired network environment

Can be shared and used by multiple app

Persistent state, you can continue unfinished tasks the next time you start

Support queue jumping, especially useful for emergency tasks

Maximum waiting queue: 20

Maximum processing queue: 3

Maximum success queue: 20

Maximum failure queue: 20.

If the above parameters do not meet your needs, you can change them directly in the code, which is located in TaskQueueManager.

How to use

If you want to embed the code in your own project, you need to change the ZlsamDownloadService project to library and refer to it in your project. The code to start and bind in the onStart callback is as follows:

Intent intent = new Intent ("com.zlsam.download.DOWNLOAD_SERVICE"); startService (intent); bindService (intent, mConnection, Context.BIND_AUTO_CREATE); IMainDownloadingService mDownloadService;private ServiceConnection mConnection = new ServiceConnection () {@ Override public void onServiceConnected (ComponentName name, IBinder service) {mDownloadService = IMainDownloadingService.Stub.asInterface (service); appendLog ("Bound to download service.");} @ Override public void onServiceDisconnected (ComponentName name) {mDownloadService = null;}}

After the binding is successful, you can call the relevant methods, which are defined in com.zlsam.download.IMainDownloadingService.

Download:

Try {int result = mDownloadService.add2Queue (url, null, null, false); if (result < 0) {appendLog ("Add task failed, error code:" + result + ", url:" + url);} else {appendLog ("Add task succeed, url:" + url);}} catch (RemoteException e) {e.printStackTrace ();}

Jump the queue to download:

Try {int result = mDownloadService.add2Queue (url, null, null, true); if (result < 0) {appendLog ("Add task jump failed, error code:" + result + ", url:" + url);} else {appendLog ("Add task jump succeed, url:" + url);}} catch (RemoteException e) {e.printStackTrace ();}

Check the status of the task:

Try {state = mDownloadService.queryState (url);} catch (RemoteException e) {e.printStackTrace (); appendLog ("Check task state: exception, url:" + url);} switch (state) {case-1: appendLog ("Check task state: not found, url:" + url); break; case 0: appendLog ("Check task state: waiting, url:" + url); break Case 1: appendLog ("Check task state: processing, url:" + url); break; case 2: appendLog ("Check task state: succeed, url:" + url); break; case 3: appendLog ("Check task state: failed, url:" + url); break;}

If you want to clear a completed (successful or failed) task (including downloaded files):

Try {mDownloadService.clearOne (mFinishedTasks.get (mFinishedTasks.size ()-1));} catch (RemoteException e) {e.printStackTrace ();}

When leaving the interface, you need to unbind the service in the onStop callback:

UnbindService (mConnection)

Debug command

Adb logcat ZlsamDownloadService:V *: s

Demo

You can install and start ZlsamDownloaderService directly with build, and the TestActivity that comes with the code is a test Demo.

The above is how to use ZlsamDownloadService. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report