During App uninstall ,How to cancel the Download?

272 Views Asked by At

In my app i have started download service,it is working fine in background.During download my testing team doing force stop and clear data or Uninstall.But After uninstall or clear data still my Download service is running in background.During download i have installed the same app again but it is misbehaving some thing.While uninstall or clear data or force stop i have to cancel the download How?

public class FileDownloaderService extends IntentService {


    private CarcarePreferences preferences;

    public FileDownloaderService() {
        super("FileDownloaderService");
    }

    @Override
    public void onCreate() {
        super.onCreate();

        preferences = CarcarePreferences.getCarcarePreferencesObject(getApplicationContext());
        DBHelper.getInstance(getApplicationContext()).open();

        downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Bundle extras = intent.getExtras();

        if (extras == null) {
            return;
        }

        if (extras.containsKey("ResultReceiver")) {
            resultReceiver = extras.getParcelable("ResultReceiver");
        }

        if (extras.containsKey("ContentToDownload")) {
            contentToDownload = extras.getInt("ContentToDownload");
        } else {
            return;
        }

        if (contentToDownload != Carcare.ContentToDownload.IMAGES) {
            isDefaultVehicle = extras.getBoolean("IsDefaultVehicle");
            fetchVehicle();
        }

        switch (contentToDownload) {
            case Carcare.ContentToDownload.HEADUNIT_IMAGES:
                if (extras.containsKey("HeadUnits")) {
                    headUnits = (ArrayList<Unit>) extras.getSerializable("Units");
                    downloadHeadUnits();
                    resultReceiver.send(0, null);
                }
                break;


        }
    }

    private void fetchVehicle() {
        Object[] objects;

        if (isDefaultVehicle) {
            objects = DBAdapter.getAllVehicles(preferences.getDefaultModel(),
                    preferences.getDefaultYear(), isDefaultVehicle);
        } else {
            objects = DBAdapter.getAllVehicles(preferences.getCurrentModel(),
                    preferences.getCurrentYear(), isDefaultVehicle);
        }

        vehicle = (Vehicle) objects[0];
    }

    private void downloadHeadUnits() {
        mHeadUnitDir = SdUtils.getDir(this);
        //clearHeadUnits();

        for (CUnit unit : Units) {

            String fileName = mDir + "/" + unit.getGuid() + ".png";
            InputStream stream = null;
            final HttpGet httpRequest = new HttpGet(unit.getHuImageUrl());
            httpRequest.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_KEEP_ALIVE);
            try {
                File file = new File(fileName);
                if (!file.exists()) {
                    FileOutputStream out = new FileOutputStream(file); //openFileOutput(fileName);
                    stream = new DefaultHttpClient().execute(httpRequest).getEntity().getContent();
                    Bitmap bitmap = BitmapFactory.decodeStream(stream);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (IllegalStateException ex) {
                ex.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void download() {
        cancelDownload(Carcare.FileType.QRG, vehicle.getPath());
        deleteDoc(vehicle.getQRGPath());

        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(vehicle.getUrl()));
        request.setDestinationUri(Uri.parse(vehicle.getPath()));
        request.setTitle("Unit");
        request.setDescription("Quick Reference Guide");

        preferences.setDownloadID(Carcare.FileType.QRG, downloadManager.enqueue(request));
    }
}
2

There are 2 best solutions below

1
On

Take a look at the remove() method of the DownloadManager.

It says:

public int remove (long... ids) Added in API level 9

Cancel downloads and remove them from the download manager. Each download will be stopped if it was running, and it will no longer be accessible through the download manager. If there is a downloaded file, partial or complete, it is deleted. Parameters ids the IDs of the downloads to remove Returns

the number of downloads actually removed

Edit

To intercept your application uninstall take a look at this answer.

1
On

You must use a Service.

In the Service's onDestroy(), you can write the code to finish the DownloadManager.

The Service will be killed before the app is about to uninstall. This way the Download will stop.