AsyncTask onPostExecute 在 doInBackground 之前调用
AsyncTask onPostExecute called before doInBackground
这个 AsyncTask 有这个问题 class:
public class DownloadPDFTask extends AsyncTask<String, Void, File> {
private Context mContext;
private String mDownloadDIR = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
public DownloadPDFTask(Context mContext) {
this.mContext = mContext;
}
@Override
protected synchronized void onPreExecute() {
super.onPreExecute();
removeOldFiles();
}
@Override
protected synchronized File doInBackground(String... args) {
File retFile = null;
downloadFilePDF(args[0]);
File directory = new File(mDownloadDIR);
File[] files = directory.listFiles();
for (File file : files) {
if (file.getName().contains(".pdf")) {
retFile = file;
break;
}
}
return retFile;
}
@Override
protected synchronized void onPostExecute(File file) {
super.onPostExecute(file);
NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancelAll();
Intent mGoToPDFView = new Intent(mContext, PDFViewerActivity.class);
mGoToPDFView.putExtra("FILE", file);
mGoToPDFView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(mGoToPDFView);
}
private void downloadFilePDF(String url) {
DownloadManager mDownloadManager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
Uri mUri = Uri.parse(url);
DownloadManager.Request mReq = new DownloadManager.Request(mUri);
mReq.setTitle("Develop Test");
mReq.setDescription("PDF downloading....");
mReq.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
mReq.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "temp.pdf");
mReq.setMimeType("application/pdf");
mDownloadManager.enqueue(mReq);
}
private void removeOldFiles() {
File mDirectory = new File(mDownloadDIR);
if (mDirectory.isDirectory()) {
String[] children = mDirectory.list();
for (String child : children) {
if (child.contains(".pdf")) {
new File(mDirectory, child).delete();
}
}
}
}
}
我想清除下载文件夹,下载 PDF 并打开其他 activity 通过 intent 创建的文件,如果我使用调试,则进程运行缓慢,但在运行时,调用 onPostExecute 方法在 doInBackground 方法之前。
你能帮帮我吗?
感谢@CommonsWare 的输入!
我用这个解决方案解决了,我改变了AsyncTask:
public class DownloadPDFTask extends AsyncTask<String, Void, Void> {
@Override
protected synchronized void onPreExecute() {
String mDownloadDIR = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
super.onPreExecute();
File mDirectory = new File(mDownloadDIR);
if (mDirectory.isDirectory()) {
String[] children = mDirectory.list();
for (String child : children) {
if (child.contains(".pdf")) {
new File(mDirectory, child).delete();
}
}
}
}
@Override
private void downloadFilePDF(String url) {
DownloadManager mDownloadManager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
Uri mUri = Uri.parse(url);
DownloadManager.Request mReq = new DownloadManager.Request(mUri);
mReq.setTitle("Develop Test");
mReq.setDescription("PDF downloading....");
mReq.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
mReq.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "temp.pdf");
mReq.setMimeType("application/pdf");
mDownloadManager.enqueue(mReq);
return null;
}
@Override
protected synchronized void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
NotificationManager mNotificationManager = (NotificationManager) BaseApplication.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancelAll();
}
}
我要使用 onPostExecute,但我正在等待带有 BroadcastReceiver 的 DownloadManager:
public class DownloadReceiver extends BroadcastReceiver {
private String mDownloadDIR = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
private Context mContext = BaseApplication.getAppContext();
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
Log.i("DownloadReceiver", "DownloadCompleted!");
File retFile = null;
File directory = new File(mDownloadDIR);
File[] files = directory.listFiles();
for (File file : files) {
Log.i("DownloadReceiver", "Searching file in DOWNLOADS");
if (file.getName().contains(".pdf")) {
Log.i("DownloadReceiver", "File found!");
retFile = file;
break;
}
}
if (retFile != null) {
Log.i("DownloadReceiver", "Go to PDF viewing...");
Intent mGoToPDFView = new Intent(mContext, PDFViewerActivity.class);
mGoToPDFView.putExtra("FILE", retFile);
mGoToPDFView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(mGoToPDFView);
}
}
}
}
AndroidManifest.xml 更新后...它有效!
这个 AsyncTask 有这个问题 class:
public class DownloadPDFTask extends AsyncTask<String, Void, File> {
private Context mContext;
private String mDownloadDIR = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
public DownloadPDFTask(Context mContext) {
this.mContext = mContext;
}
@Override
protected synchronized void onPreExecute() {
super.onPreExecute();
removeOldFiles();
}
@Override
protected synchronized File doInBackground(String... args) {
File retFile = null;
downloadFilePDF(args[0]);
File directory = new File(mDownloadDIR);
File[] files = directory.listFiles();
for (File file : files) {
if (file.getName().contains(".pdf")) {
retFile = file;
break;
}
}
return retFile;
}
@Override
protected synchronized void onPostExecute(File file) {
super.onPostExecute(file);
NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancelAll();
Intent mGoToPDFView = new Intent(mContext, PDFViewerActivity.class);
mGoToPDFView.putExtra("FILE", file);
mGoToPDFView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(mGoToPDFView);
}
private void downloadFilePDF(String url) {
DownloadManager mDownloadManager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
Uri mUri = Uri.parse(url);
DownloadManager.Request mReq = new DownloadManager.Request(mUri);
mReq.setTitle("Develop Test");
mReq.setDescription("PDF downloading....");
mReq.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
mReq.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "temp.pdf");
mReq.setMimeType("application/pdf");
mDownloadManager.enqueue(mReq);
}
private void removeOldFiles() {
File mDirectory = new File(mDownloadDIR);
if (mDirectory.isDirectory()) {
String[] children = mDirectory.list();
for (String child : children) {
if (child.contains(".pdf")) {
new File(mDirectory, child).delete();
}
}
}
}
}
我想清除下载文件夹,下载 PDF 并打开其他 activity 通过 intent 创建的文件,如果我使用调试,则进程运行缓慢,但在运行时,调用 onPostExecute 方法在 doInBackground 方法之前。
你能帮帮我吗?
感谢@CommonsWare 的输入!
我用这个解决方案解决了,我改变了AsyncTask:
public class DownloadPDFTask extends AsyncTask<String, Void, Void> {
@Override
protected synchronized void onPreExecute() {
String mDownloadDIR = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
super.onPreExecute();
File mDirectory = new File(mDownloadDIR);
if (mDirectory.isDirectory()) {
String[] children = mDirectory.list();
for (String child : children) {
if (child.contains(".pdf")) {
new File(mDirectory, child).delete();
}
}
}
}
@Override
private void downloadFilePDF(String url) {
DownloadManager mDownloadManager = (DownloadManager) mContext.getSystemService(Context.DOWNLOAD_SERVICE);
Uri mUri = Uri.parse(url);
DownloadManager.Request mReq = new DownloadManager.Request(mUri);
mReq.setTitle("Develop Test");
mReq.setDescription("PDF downloading....");
mReq.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
mReq.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "temp.pdf");
mReq.setMimeType("application/pdf");
mDownloadManager.enqueue(mReq);
return null;
}
@Override
protected synchronized void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
NotificationManager mNotificationManager = (NotificationManager) BaseApplication.getAppContext().getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancelAll();
}
}
我要使用 onPostExecute,但我正在等待带有 BroadcastReceiver 的 DownloadManager:
public class DownloadReceiver extends BroadcastReceiver {
private String mDownloadDIR = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString();
private Context mContext = BaseApplication.getAppContext();
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
Log.i("DownloadReceiver", "DownloadCompleted!");
File retFile = null;
File directory = new File(mDownloadDIR);
File[] files = directory.listFiles();
for (File file : files) {
Log.i("DownloadReceiver", "Searching file in DOWNLOADS");
if (file.getName().contains(".pdf")) {
Log.i("DownloadReceiver", "File found!");
retFile = file;
break;
}
}
if (retFile != null) {
Log.i("DownloadReceiver", "Go to PDF viewing...");
Intent mGoToPDFView = new Intent(mContext, PDFViewerActivity.class);
mGoToPDFView.putExtra("FILE", retFile);
mGoToPDFView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(mGoToPDFView);
}
}
}
}
AndroidManifest.xml 更新后...它有效!