洞察探索open banking如何通过小程序容器技术助力金融企业实现数据安全和数字化转型
1008
2022-10-22
Android中更好的应用程序-管理器
Fetch
A better in app Download Manager for Android.
Overview
Fetch is a simple yet powerful Android library that allows you to manage downloads more efficiently in your Android apps. It uses a background service on the device to download and maintain requests.
Features
Simple and easy to use API.Continuous downloading in the background.Concurrent Downloads Support.Ability to pause and resume downloads.Set the priority of a download request.Ability to retry failed downloads.Easy progress and status tracking.And more...
Prerequisites
If you are saving downloads outside of your application's sandbox, you will need to add the following storage permissions to your application's manifest. For Android SDK version 23(M) and above, you will also need to explicitly request these permissions from the user.
How to use Fetch
Using Fetch is easy! Just add the Gradle dependency to your application's build.gradle file.
compile 'com.tonyodev.fetch:fetch:1.1.5'
Next, get an instance of Fetch and request a download. A unique ID will be returned for each request.
public class MainActivity extends AppCompatActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Fetch fetch = Fetch.newInstance(this); ... Request request = new Request(url,dirPath,fileName); long downloadId = fetch.enqueue(request); if(downloadId != Fetch.ENQUEUE_ERROR_ID) { //Download was successfully queued for download. } }}
Tracking a download's progress and status is very easy with Fetch. Simply add a FetchListener to your instance, and the listener will be notified whenever a download's status or progress changes.
fetch.addFetchListener(new FetchListener() { @Override public void onUpdate(long id, int status, int progress, long downloadedBytes, long fileSize, int error) { if(downloadId == id && status == Fetch.STATUS_DOWNLOADING) { progressBar.setProgress(progress); ... }else if(error != Fetch.NO_ERROR) { //An error occurred if(error == Fetch.ERROR_HTTP_NOT_FOUND) { //handle error } } } });
Fetch supports pausing and resuming downloads. If a download request fails, you can opt to retry.
...fetch.pause(downloadId);...fetch.resume(downloadId);...fetch.retry(downloadId);
When you are done with an instance of Fetch, simply release it. It is important that you release instances of fetch to prevent memory leaks.
//do workfetch.release();//do more work
Changing Fetch settings are easy!
new Fetch.Settings(getApplicationContext()) .setAllowedNetwork(Fetch.NETWORK_ALL) .enableLogging(true) .setConcurrentDownloadsLimit(3) .apply();
Contribute
Fetch can only get better if you make code contributions. Found a bug? Report it. Have a feature idea you'd love to see in Fetch? Contribute to the project!
License
Copyright (C) 2017 Tonyo Francis.Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at http://apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~