我有一系列Urls,我会同步。 事实上,我正在使用for循环进行asynctask。 我尝试使用downloadmanager和广播接收器没有成功(管理器使用另一个线程,所以我不能每次取消注册,泄漏附加)。
这是我的Asynctask:
package com.ylly.hypred.splashScreen.asynctask;
import android.app.DownloadManager;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.util.Log;
import android.view.WindowManager;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.ylly.hypred.R;
import com.ylly.hypred.api.CallAPI;
import com.ylly.hypred.api.model.MediaAPIModel;
import com.ylly.hypred.api.model.MediaArrayAPIModel;
import com.ylly.hypred.utils.FileManager;
import java.io.File;
import java.util.ArrayList;
/**
* Created by YLLY on 17-11-2015.
*/
public class LoadMedias extends AsyncTask<String, Void, String> {
private MediaArrayAPIModel mediaAPIModels;
private Context context;
private ProgressDialog progressDialog;
private boolean isCallFromUpdate;
private final String TAG = "LoadMedias";
private OnLoadingIsFinished mCallback;
/**
*
*/
public interface OnLoadingIsFinished {
void callProcess();
}
/**
*
* @param context
* @param urls
* @param isCallFromUpdate
*/
public LoadMedias(Context context, MediaArrayAPIModel urls, boolean isCallFromUpdate) {
this.context = context;
this.mediaAPIModels = urls;
progressDialog = new ProgressDialog(context);
this.isCallFromUpdate = isCallFromUpdate;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
if (!isCallFromUpdate)
progressDialog.setTitle(context.getString(R.string.fm_etape_trois_sur_trois));
progressDialog.setMessage(context.getString(R.string.fm_enregistrement));
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(false);
progressDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
progressDialog.setMax(mediaAPIModels.getMediaImagePIModels().size() + mediaAPIModels.getMediaFileAPIModels().size());
progressDialog.setProgress(0);
progressDialog.show();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
mCallback.callProcess();
}
@Override
protected String doInBackground(String... arg0) {
try {
loadMediaImages(mediaAPIModels.getMediaImagePIModels(), progressDialog);
loadMediaFiles(context, mediaAPIModels.getMediaFileAPIModels(), progressDialog);
} catch (Exception e) {
Log.d(TAG, e.toString());
}
return "";
}
/**
*
* @param onLoadingIsFinished
*/
public void setOnLoadingIsFinished(OnLoadingIsFinished onLoadingIsFinished) {
this.mCallback = onLoadingIsFinished;
}
/**
* @param mediaAPIModels
* @param progressDialog
*/
private void loadMediaImages(ArrayList<MediaAPIModel> mediaAPIModels, ProgressDialog progressDialog) {
String type;
for (int i = 0; i < mediaAPIModels.size(); i++) {
type = mediaAPIModels.get(i).getUrl().substring(mediaAPIModels.get(i).getUrl().lastIndexOf("."));
if (type.equals(".png") || type.equals(".jpg") || type.equals(".jpeg")) {
ImageLoader.getInstance().loadImageSync(CallAPI.mBaseUrl + mediaAPIModels.get(i).getUrl());
i++;
progressDialog.setProgress(progressDialog.getProgress() + 1);
}
}
}
/**
* @param context
* @param mediaAPIModels
* @param progressDialog
*/
private void loadMediaFiles(Context context, ArrayList<MediaAPIModel> mediaAPIModels, final ProgressDialog progressDialog) {
{
String name;
String mime;
for (int i = 0; i < mediaAPIModels.size(); i++) {
if (!FileManager.getInstance().getFileFromLocal(context, mediaAPIModels.get(i).getUrl(), false)) {
name = mediaAPIModels.get(i).getUrl().substring(mediaAPIModels.get(i).getUrl().lastIndexOf("/") + 1); //on sépare le nom du fichier du reste de l'url
mime = name.substring(name.lastIndexOf(".")); //on recupere l'extension du fichier
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(CallAPI.mBaseUrl + mediaAPIModels.get(i).getUrl()));
request.setDescription(context.getResources().getString(R.string.downloading) + " name");
request.setTitle(context.getResources().getString(R.string.app_name));
request.setMimeType(mime);
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir("test" + File.separatorChar + "doc", name);
// get download service and enqueue file
DownloadManager manager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
context.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
context.unregisterReceiver(this);
Log.d("DOWNLOAD", "unregister");
progressDialog.setProgress(progressDialog.getProgress() + 1);
}
}, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
} else {
i++;
progressDialog.setProgress(progressDialog.getProgress() + 1);
}
}
}
}
}
缓存加载有效。
提前致谢;)