Android:如何避免再次下载当前正在下载的文件

时间:2017-05-17 23:12:52

标签: java android android-file android-download-manager

public class RemoteApkInstaller {
    public void install(String url) {
        listenForDownloadCompleteEvent();
        downloadApk(url);
    }

    private void listenForDownloadCompleteEvent() {
        IntentFilter downloadCompleteIntentFilter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        downloadCompleteReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (isInitiatedApkDownload(intent)) {
                    if (isDownloadSuccessful()) {
                        handleDownloadComplete();
                    } else {
                        Toast.makeText(context, R.string.download_error, Toast.LENGTH_SHORT).show();
                        logFailure();
                    }
                    context.unregisterReceiver(downloadCompleteReceiver);
                }
            }
        };
        context.registerReceiver(downloadCompleteReceiver, downloadCompleteIntentFilter);
    }

    private void downloadApk(String url) {
        Uri uri = Uri.parse(url);
        apkFileName = uri.getLastPathSegment();
        if (isFileExistsInDownloadDir()) {
            handleDownloadComplete();
            return;
        }
        DownloadManager.Request request = getDownloadRequest(uri);
        downloadID = getDownloadManager().enqueue(request);
    }
    private boolean isFileExistsInDownloadDir() {
        File directory = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
        File file = new File(directory, apkFileName);
        return file.exists();
    }

    private void handleDownloadComplete() {
        doOperationsOnDownloadedPackage()
        installApk(context);
    }

    private void doOperationsOnDownloadedPackage() {
        PackageManager packageManager= getApplicationContext().getPackageManager();
        String appName = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA));
        ...
    }

    private void installApk(Context context) {
        File directory = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
        File file = new File(directory, apkFileName);

        String mimeType = downloadID == UNSET_DOWNLOAD_ID ? MIME_TYPE_FOR_APK_FILE : getDownloadManager().getMimeTypeForDownloadedFile(downloadID);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            Intent installIntent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
            Uri apkUri = FileProvider.getUriForFile(context, "my.package.fileProvider", file);
            installIntent.setDataAndType(apkUri, mimeType);
            installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            context.startActivity(installIntent);
        } else {
            Uri apkUri = Uri.fromFile(file);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(apkUri, mimeType);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    }
}

基本上,当我单击weblink时,会创建此类的实例并调用install()方法。问题是当我再次单击weblink而前一次下载仍然只是部分完成时。我正在尝试检查下载文件夹以确定文件是否已存在,如果是,请继续执行下一组操作。 即使先前的下载部分完成,标志也是如此,所以在这种情况下,我正在对不是100%下载的apk文件执行doOperationsOnDownloadedPackage()方法,我看到显然崩溃了。 我正在寻找的是一种避免在当前正在下载相同文件时启动重复下载的方法。 任何帮助赞赏。 谢谢

0 个答案:

没有答案
相关问题