【问题标题】:During App uninstall ,How to cancel the Download?App卸载过程中,如何取消下载?
【发布时间】:2014-11-25 14:14:54
【问题描述】:

在我的应用程序中,我已经启动了下载服务,它在后台运行良好。在下载期间,我的测试团队强制停止并清除数据或卸载。但在卸载或清除数据后,我的下载服务仍在后台运行。下载期间我再次安装了相同的应用程序,但它出现了一些问题。在卸载或清除数据或强制停止时,我必须取消下载如何?

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));
    }
}

【问题讨论】:

  • 卸载服务正在运行吗? 做梦都做不到。卸载应用时,与应用相关的所有资源瞬间被删除..
  • @nobalG 它正在发生,仅通过使用意图服务我已经启动了该服务
  • 你能想出一些你是如何开始下载的源代码吗?
  • @PRavikant 更新了我的代码
  • 使用标志 stopWithTask="true" 用于清单中的服务

标签: android android-service background-process android-download-manager


【解决方案1】:

您必须使用Service

在Service的onDestroy()中,可以编写代码完成DownloadManager

Service 将在应用程序即将卸载之前被杀死。 这样下载就会停止。

【讨论】:

  • 服务卸载时如何调用ondestroy方法
【解决方案2】:

看看DownloadManager的remove()方法。

上面写着:

public int remove (long... ids) 在 API 级别 9 中添加

取消下载并将其从下载管理器中删除。每个 如果正在运行,下载将停止,并且将不再 可通过下载管理器访问。如果有下载 文件,部分或完整,它被删除。参数 ids 的 ID 要删除的下载 Returns

the number of downloads actually removed

编辑

要拦截您的应用程序卸载,请查看此answer

【讨论】:

  • @Boopathi 我已经编辑了我的答案,包括如何拦截应用程序 unistall 看看这是否适合你。
猜你喜欢
  • 2013-10-10
  • 2012-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多