I am using Asynctask to download files in my Android app. As the file size is generally large, I start the file download in the background. There is no progressbar or anything shown so that the user can access other parts of the app while the download is in progress.
On completion of file download, I want to inform the user that his file has been successfully downloaded.
Using onPostexecute() method creates problems because the user is no longer on the same activity when the download process began. As a result I cannot use Alert dialog & Notifications from onPostExecute() as there are problems about the context.
I have pasted below the code for my Asynctask. So, kindly suggest me a way to fix this.
class DownloadProcess extends AsyncTask<Void,Void,Void>{
Context cxt;
public DownloadProcess(Context context) {
cxt=context;
}
@Override
protected Void doInBackground(Void... arg0) {
try {
String fileurl="http://www.bigfiles/" + filenm;
URL url = new URL(fileurl);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
Path = Environment.getExternalStorageDirectory() + "/download/";
File pth=new File(Path);
File file = new File(pth,filenm);
FileOutputStream fos = new FileOutputStream(file);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
} catch (IOException e) {
Log.d("amit", "Error: " + e);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (((Activity) AppDet.this).isFinishing() == false) {
AlertDialog.Builder builder = new AlertDialog.Builder(AppDet.this);
builder.setMessage("The app download is complete. Please check " +Path+ " on your device and open the " + filenm+ " file downloaded");
builder.setCancelable(true);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
MediaPlayer md=MediaPlayer.create(AppDet.this, R.raw.ding);
md.start();
}
else {
//Not sure what to do here
}
}