Java Android:如何避免再次下载当前正在下载的文件
基本上,当我单击一个weblink时,就会创建这个类的一个实例并调用install()方法。问题是,当我再次单击Web链接时,上一次下载仍然只是部分完成。我正在尝试检查下载文件夹,以确定文件是否已经存在,如果已经存在,请继续执行下一组操作。 即使上一次下载部分完成,该标志也为true,因此在本例中,我对一个未100%下载的apk文件执行doOperationsOnDownloadedPackage()方法,我看到明显的崩溃。 我要寻找的是一种方法,如果同一个文件当前正在下载,则可以避免启动重复下载。 谢谢你的帮助。Java Android:如何避免再次下载当前正在下载的文件,java,android,android-file,android-download-manager,Java,Android,Android File,Android Download Manager,基本上,当我单击一个weblink时,就会创建这个类的一个实例并调用install()方法。问题是,当我再次单击Web链接时,上一次下载仍然只是部分完成。我正在尝试检查下载文件夹,以确定文件是否已经存在,如果已经存在,请继续执行下一组操作。 即使上一次下载部分完成,该标志也为true,因此在本例中,我对一个未100%下载的apk文件执行doOperationsOnDownloadedPackage()方法,我看到明显的崩溃。 我要寻找的是一种方法,如果同一个文件当前正在下载,则可以避免启动重复下
谢谢将标志保存到共享首选项。如果有其他可能的方法,欢迎使用。共享首选项标志也很好,但是有太多的边缘情况,如用户取消下载、下载出错等,我必须跟踪更新标志,否则下一次下载不会发生。内存中的标志也可以工作。在活动或片段中,只需设置一个布尔值,然后在下载完成之前不响应click事件。如果它是一个常规布局,而不是一个网络视图,我会说禁用按钮,但这可能不是一个选项。
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);
}
}
}