简体   繁体   中英

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.

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) {

        }



    }

如果您使用服务,则可以通过意图(启动服务的意图)传递字符串。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM