Giter Site home page Giter Site logo

Comments (3)

tonyofrancis avatar tonyofrancis commented on May 15, 2024

Fetch will continue to download the file in the background. If you intent service is killed, The instance of Fetch will no longer receive status updates via the Fetch Listener but the download will still be processing in the background. If you would like to share your code or an example, I would be glad to have a look.

from fetch.

ahshoaib avatar ahshoaib commented on May 15, 2024

Thanks for the reply. Sorry for not being clear! when I start download and then I press the home button my app goes in the background and the download continues, but when I kill the app from background ( Keeping the home button pressed and then killing the process) the download stops. I have written a simple download program that continues downloading even if I kill the app process, but that program is not very stable and useful so I decided to use Fetch.
The following the my code snippet.

public void startDownload(final String name, final String packageName, final String path, final boolean obb, final boolean data) {
        try {
            final Fetch fetch = Fetch.getInstance(this);
            Intent i = new Intent(getApplicationContext(), DownloadListView.class);
            PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);

            mBuilder = new NotificationCompat.Builder(this);
            mBuilder.setContentTitle(name)
                    .setContentText("Downloading")
                    .setSmallIcon(R.drawable.image)
                    .setContentInfo("0%")
                    .setProgress(100, 0, true)
                    .setContentIntent(pi)
                    .setOngoing(true)
                    .setAutoCancel(true);

            nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notification = mBuilder.build();
            
            //this method is used to run the app in foreground even if it is killed from background
            startForeground(notificationId, notification);

            url = IPClass.SERVERIP + path + "/" + packageName;
            String dirPath = "/sdcard/Android/appdata/tmp/downloadtmp";

            Request request = new Request(url, dirPath, packageName);

            if (fetch.contains(request)) {
            		//getting the download id from database
                fetch.resume(new DatabaseTools(getApplicationContext()).getFetchId(packageName));
            }else{
                downloadId = fetch.enqueue(request);
                //stroing the downloadId to the database for future use
                new DatabaseTools(getApplicationContext()).setDownloadID(packageName, downloadId);
            }

            fetch.addFetchListener(new FetchListener() {
                @Override
                public void onUpdate(long id, int status, int progress, long downloadedBytes, long fileSize, int error) {

                    if (progress < 100) {
                        if (downloadId == id && status == Fetch.STATUS_DOWNLOADING) {
                            int downloadedKB = (int) downloadedBytes / 1024;
                            if (downloadedKB < 1024) {
                                mBuilder.setProgress(100, Integer.valueOf(progress), false).setContentInfo(progress + "%" + "  |  " + downloadedKB + " KB");
                                nm.notify(notificationId, mBuilder.build());
                            } else {
                                float downloadedMB = (float) downloadedKB / 1024;
                                double dMB = (float) Math.round(downloadedMB * 100.0) / 100.0;

                                mBuilder.setProgress(100, Integer.valueOf(progress), false).setContentInfo(progress + "%" + "  |  " + dMB + " MB");
                                nm.notify(notificationId, mBuilder.build());
                            }
                        }
                    } else {

                        Long tmpid = new DatabaseTools(getApplicationContext()).getFetchId(packageName);
                        fetch.remove(tmpid);
                        //calling asynch task to unzip the downloaded file if it is .zip otherwise prompt user for installation
                        new DownloadFinished(name, packageName, obb, data).execute();
                    }
                }
            });

        } catch (Exception ex) {
        } finally {
			fetch.release();
       }
    }

Also another small problem I am facing is when all downloads are stopped by any reason and I start a new download the new download goes to the queue, but Fetch starts downloading a previously enqueued one!

Thanks for the help!

from fetch.

tonyofrancis avatar tonyofrancis commented on May 15, 2024

@ahshoaib sorry for the late reply. When you kill an application's process, everything that runs in the process is killed(The way it suppose to work). Fetch does not run on an independent process. It runs on your applications process, so it will be killed and downloading will stop.

What you are describing seems more like an application design issue. What I recommend doing is scheduling an alarm, Job Service or broadcast receiver that can start the Fetch Service in the background and continue to download requests. To do this you can simply call Fetch.startService(context);(Note this is called on the Fetch class) from the component to start downloading in the background. I would also recommend putting Fetch.startService(context); in your Application class onCreate method. That way, whenever your application process starts, the FetchService starts.

For your second question, Fetch is a queue based downloading service. The first request in the queue will get processed first, then the next and so on. Fetch does not currently support multi downloading. Fetch does support priority downloading. If there is a download that you want to execute immediately without waiting in the queue. you can call fetch.setPriority(downloadId, Fetch.PRIORITY_HIGH);. You can also set the priority when creating a request:

Request request = new Request(url,filePath);
request.setPriority(Fetch.PRIORITY_HIGH);
fetch.enqueue(request);

Note that if you enqueue several request with Fetch.PRIORITY_HIGH they all will be downloaded in the order they were enqueued but will be downloaded before Fetch.PRIORITY_NORMAL requests.

If you would like more control of the FetchService, you can interact with it via intents and broadcast receivers. I have not documented the FetchService class yet, but it is simple enough to follow along. Hope this helps.

from fetch.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.