Narendra Singh
Narendra Singh

Reputation: 4001

downloading terminates when App goes to background

I am working on an App, that uses phonegap.

When the App is started for the first time, I am downloading the data from the server. That downloading process is being executed in AsyncTask, and in onPostExecute() of that, I am loading the url.

The problem being arose is that, when my download process is getting executed, and the App somehow goes to the background, the downloading gets terminated, unfortunately, which is absolutely not required.

To overcome this trouble; I guess, using service is one of the good options. But, in that case, another question arises, that is, how can I possibly, load the url inside service.

Otherwise, what else can be done?

Please, suggest me the possible ways to get rid off this trouble, I am currently facing.

Upvotes: 0

Views: 146

Answers (2)

user2441595
user2441595

Reputation:

Use the similar method for downloading the data. This may be helpful.

     public void downloadUsingGET(String apkurl, String fileName) {

        try {
            String PATH = Environment.getExternalStorageDirectory()+"/Android/data/"+getPackageName()+"/files/";
            URL url = new URL(apkurl);
            HttpURLConnection c = (HttpURLConnection) url.openConnection();
            c.setRequestMethod("GET");
            c.connect();
            File file = new File(PATH);
            file.mkdirs();
            File outputFile = new File(file, fileName);
            FileOutputStream fos = new FileOutputStream(outputFile);

            InputStream is = c.getInputStream();

            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = is.read(buffer)) != -1) {
                fos.write(buffer, 0, len1);
            }
            fos.close();
            is.close();

        } catch (IOException e) {

        }



    }

Upvotes: 1

Nick
Nick

Reputation: 575

If you use a service, you can pass strings trough an intent(the intent you started the service).

Upvotes: 0

Related Questions