final Button btnd = (Button)findViewById(R.id.button1); btnd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Animation anim=AnimationUtils.loadAnimation(Mabhas1.this, R.anim.anima); btnd.startAnimation(anim); mProgressDialog = new ProgressDialog(Mabhas1.this); mProgressDialog.setMessage("در حاف دالفند..."); mProgressDialog.setIndeterminate(true); mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); mProgressDialog.setCancelable(true); final DownloadTask downloadTask = new DownloadTask(Mabhas1.this); downloadTask.execute("http://bayanbox.ir/id/6955356415739458249?download"); mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { downloadTask.cancel(true); } }); } class DownloadTask extends AsyncTask { private Context context; public DownloadTask(Context context) { this.context = context; } @Override protected String doInBackground(String... sUrl) { PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getClass().getName()); wl.acquire(); try { InputStream input = null; OutputStream output = null; HttpURLConnection connection = null; try { URL url = new URL(sUrl[0]); connection = (HttpURLConnection) url.openConnection(); connection.connect(); if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) return "Server returned HTTP " + connection.getResponseCode() + " " + connection.getResponseMessage(); int fileLength = connection.getContentLength(); input = connection.getInputStream(); output = new FileOutputStream("/storage/sdcard0/TanhaMasir/tanha1.mp3"); // /sdcard/file_name.extension byte data[] = new byte[4096]; long total = 0; int count; while ((count = input.read(data)) != -1) { if (isCancelled()) return null; total += count; if (fileLength > 0) publishProgress((int) (total * 100 / fileLength)); output.write(data, 0, count); } } catch (Exception e) { return e.toString(); } finally { try { if (output != null) output.close(); if (input != null) input.close(); } catch (IOException ignored) { } if (connection != null) connection.disconnect(); } } finally { wl.release(); } return null; } @Override protected void onPreExecute() { super.onPreExecute(); mProgressDialog.show(); } @Override protected void onProgressUpdate(Integer... progress) { super.onProgressUpdate(progress); mProgressDialog.setIndeterminate(false); mProgressDialog.setMax(100); mProgressDialog.setProgress(progress[0]); } @Override protected void onPostExecute(String result) { mProgressDialog.dismiss(); if (result != null) Toast.makeText(context,"خظا در دالفند: "+result, Toast.LENGTH_LONG).show(); else Toast.makeText(context,"دالفند شد", Toast.LENGTH_SHORT).show(); } } });