Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Intenet Service startForeground(notificationId, notification); #16

Closed
ahshoaib opened this issue Mar 13, 2017 · 3 comments
Closed

Intenet Service startForeground(notificationId, notification); #16

ahshoaib opened this issue Mar 13, 2017 · 3 comments
Assignees

Comments

@ahshoaib
Copy link

Hello!

I am using fetch on the intent service. on the intent service I am calling startForeground method. The problem is when I close my app the download also stops. As I expect it should continue the download.

Thanks

@tonyofrancis
Copy link
Owner

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.

@ahshoaib
Copy link
Author

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!

@tonyofrancis
Copy link
Owner

tonyofrancis commented Mar 15, 2017

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants