我想在失去联系时取消下载文件由于无线网络,我在下载文件时失去联系在这里的问题,但是当我再次单击下载文件时这里的问题没有上传,并且我有空白页Internet丢失的情况下如何取消下载
我的代码
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt1:
TextView etName1 = (TextView) v.findViewById(R.id.textView1);
if (!isNetworkAvailable()) {
Toast.makeText(getActivity().getApplicationContext(), "لا يوجد اتصال بالانترنت لا يمكن فتح الصفحة", Toast.LENGTH_SHORT).show();
return;
}
apkStorage1 = getActivity().getApplicationContext().getDir(
"NKDROID FILES", Context.MODE_PRIVATE);
outputFile1 = new File(apkStorage1, "/kims5f31.pdf");
if (outputFile1.exists()) {
Intent intent = new Intent(kim3sf5.this.getActivity(), PDFActivity.class).putExtra(PDFActivity.EXTRA_TAG, "kims5f31.pdf");
intent.putExtra("name", etName1.getText());
startActivity(intent);
} else {
if (!isNetworkAvailable()) {
Toast.makeText(getActivity().getApplicationContext(), "لا يوجد اتصال بالانترنت لتحميل الملف", Toast.LENGTH_SHORT).show();
return;
}
kim3sf5.DownloadTask book = new kim3sf5.DownloadTask(getActivity().getApplicationContext(), "https://mdaresalmjd.com/awas1/kims5f31.pdf");
}
break;
case R.id.bt2:
if (!isNetworkAvailable()) {
Toast.makeText(getActivity().getApplicationContext(), "لا يوجد اتصال بالانترنت لا يمكن فتح الصفحة", Toast.LENGTH_SHORT).show();
return;
}
TextView etName2 = (TextView) v.findViewById(R.id.textView2);
apkStorage1 = getActivity().getApplicationContext().getDir(
"NKDROID FILES", Context.MODE_PRIVATE);
outputFile1 = new File(apkStorage1, "/kims5f32.pdf");
if (outputFile1.exists()) {
Intent intent = new Intent(kim3sf5.this.getActivity(), PDFActivity.class).putExtra(PDFActivity.EXTRA_TAG, "kims5f32.pdf");
intent.putExtra("name", etName2.getText());
startActivity(intent);
} else {
if (!isNetworkAvailable()) {
Toast.makeText(getActivity().getApplicationContext(), "لا يوجد اتصال بالانترنت لتحميل الملف", Toast.LENGTH_SHORT).show();
return;
}
kim3sf5.DownloadTask book = new kim3sf5.DownloadTask(getActivity().getApplicationContext(), "https://.com//kims5f32.pdf");
}
break;
case R.id.bt3:
if (!isNetworkAvailable()) {
Toast.makeText(getActivity().getApplicationContext(), "لا يوجد اتصال بالانترنت لا يمكن فتح الصفحة", Toast.LENGTH_SHORT).show();
return;
}
TextView etName3 = (TextView) v.findViewById(R.id.textView3);
apkStorage1 = getActivity().getApplicationContext().getDir(
"NKDROID FILES", Context.MODE_PRIVATE);
outputFile1 = new File(apkStorage1, "/kims5f33.pdf");
if (outputFile1.exists()) {
Intent intent = new Intent(kim3sf5.this.getActivity(), PDFActivity.class).putExtra(PDFActivity.EXTRA_TAG, "kims5f33.pdf");
intent.putExtra("name", etName3.getText());
startActivity(intent);
} else {
if (!isNetworkAvailable()) {
Toast.makeText(getActivity().getApplicationContext(), "لا يوجد اتصال بالانترنت لتحميل الملف", Toast.LENGTH_SHORT).show();
return;
}
kim3sf5.DownloadTask book = new kim3sf5.DownloadTask(getActivity().getApplicationContext(), "https://.com//kims5f33.pdf");
}
break;
case R.id.bt4:
if (!isNetworkAvailable()) {
Toast.makeText(getActivity().getApplicationContext(), "لا يوجد اتصال بالانترنت لا يمكن فتح الصفحة", Toast.LENGTH_SHORT).show();
return;
}
TextView etName4 = (TextView) v.findViewById(R.id.textView4);
apkStorage1 = getActivity().getApplicationContext().getDir(
"NKDROID FILES", Context.MODE_PRIVATE);
outputFile1 = new File(apkStorage1, "/kims5f34.pdf");
if (outputFile1.exists()) {
Intent intent = new Intent(kim3sf5.this.getActivity(), PDFActivity.class).putExtra(PDFActivity.EXTRA_TAG, "kims5f34.pdf");
intent.putExtra("name", etName4.getText());
startActivity(intent);
} else {
if (!isNetworkAvailable()) {
Toast.makeText(getActivity().getApplicationContext(), "لا يوجد اتصال بالانترنت لتحميل الملف", Toast.LENGTH_SHORT).show();
return;
}
kim3sf5.DownloadTask book = new kim3sf5.DownloadTask(getActivity().getApplicationContext(), "https://.com//kims5f34.pdf");
}
}
}
public class DownloadTask {
private static final String TAG = "Download Task";
private Context context;
private String downloadUrl = "", downloadFileName = "";
private ProgressDialog progressDialog;
private boolean continueDownload = true;
public void setContinueDownload(boolean continueDownload) {
this.continueDownload = continueDownload;
}
public DownloadTask(Context context, String downloadUrl) {
this.context = context;
this.downloadUrl = downloadUrl;
continueDownload = true;
downloadFileName = downloadUrl.substring(downloadUrl.lastIndexOf('/'), downloadUrl.length());
Log.e(TAG, downloadFileName);
new kim3sf5.DownloadTask.DownloadingTask().execute();
}
private class DownloadingTask extends AsyncTask<Void, Integer, Void> {
File apkStorage = null;
File outputFile = null;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(kim3sf5.this.getActivity(), AlertDialog.THEME_HOLO_LIGHT);
progressDialog.setMessage("يتم تحميل الملف يرجى الانتظار ......");
progressDialog.setIndeterminate(true);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(true);
progressDialog.setProgress(0);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "إلغاء التحميل", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
kim3sf5.DownloadTask.DownloadingTask.this.cancel(true);
kim3sf5.DownloadTask.this.setContinueDownload(false);
dialog.dismiss();
}
});
progressDialog.show();
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
progressDialog.setIndeterminate(false);
progressDialog.setMax(100);
progressDialog.setProgress(progress[0]);
}
@Override
protected void onPostExecute(Void result) {
progressDialog.dismiss();
final Intent intent = new Intent(kim3sf5.this.getActivity(), PDFActivity.class);
intent.putExtra(PDFActivity.EXTRA_TAG, downloadFileName.substring(1));
if (downloadUrl.equals("https://.com/kims5f34.pdf"))
{
TextView etName4 = (TextView) getActivity().findViewById(R.id.textView4);
intent.putExtra("name", etName4.getText());
}
if (downloadUrl.equals("https://.com/kims5f33.pdf"))
{
TextView etName3 = (TextView) getActivity().findViewById(R.id.textView3);
intent.putExtra("name", etName3.getText());
}
if (downloadUrl.equals("https://.com/kims5f32.pdf"))
{
TextView etName2 = (TextView)getActivity().findViewById(R.id.textView2);
intent.putExtra("name", etName2.getText());
}
if (downloadUrl.equals("https://.com/kims5f31.pdf"))
{
TextView etName1 = (TextView) getActivity().findViewById(R.id.textView1);
intent.putExtra("name", etName1.getText());
}
startActivity(intent);
}
@Override
protected Void doInBackground(Void... arg0) {
try {
URL url = new URL(downloadUrl);//Create Download URL
HttpURLConnection c = (HttpURLConnection) url.openConnection();//Open Url Connection
c.setRequestMethod("GET");//Set Request Method to "GET" since we are grtting data
c.connect();//connect the URL Connection
final int fileLength = c.getContentLength();
Log.e(TAG, "fileLength " + fileLength);
if (c.getResponseCode() != HttpURLConnection.HTTP_OK) {
Log.e(TAG, "Server returned HTTP " + c.getResponseCode()
+ " " + c.getResponseMessage());
}
//Get File if SD card is present
if (new CheckForSDCard().isSDCardPresent()) {
apkStorage = getActivity().getApplicationContext().getDir(
"NKDROID FILES", Context.MODE_PRIVATE);
} else
Toast.makeText(context, "Tidak ada SD Card.", Toast.LENGTH_SHORT).show();
//If File is not present create directory
if (!apkStorage.exists()) {
apkStorage.mkdir();
Log.e(TAG, "Directory Created.");
}
outputFile = new File(apkStorage, downloadFileName);//Create Output file in Main File
//Create New File if not present
if (!outputFile.exists()) {
outputFile.createNewFile();
Log.e(TAG, "File Created");
}
FileOutputStream fos = new FileOutputStream(outputFile);//Get OutputStream for NewFile Location
InputStream is = c.getInputStream();//Get InputStream for connection
byte[] buffer = new byte[1024];//Set buffer type
int len1 = 0;//init length
long total = 0;
while (continueDownload && (len1 = is.read(buffer)) != -1) {
total += len1;
final long total_tmp = total;
Log.e(TAG, "progressDialog " + (total * 100 / fileLength));
publishProgress((int) (total * 100 / fileLength));
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.setProgress((int) (total_tmp * 100 / fileLength));
}
});
fos.write(buffer, 0, len1);//Write new file
}
if (!continueDownload && outputFile.exists()) {
try {
outputFile.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
//Close all connection after doing task
fos.close();
is.close();
} catch (Exception e) {
//Read exception if something went wrong
e.printStackTrace();
outputFile = null;
Log.e(TAG, "Download Error Exception " + e.getMessage());
}
return null;`enter code here`
}
}