۱۳۹۴/۰۱/۰۱, ۱۱:۳۶ ق.ظ
با سلام و خسته نباشید خدمت مدیر محترم.
من نرم افزاری دارم طبق آموزش های قبلی شما در اون کدی استفاده کردم که فایل رو دانلود می کنه و در حافظه ی گوشی ذخیره می کنه.
فقط مشکل اینجاست که وقتی کاربر روی صفحه (خارج از دیالوگ بار) کلیک می کنه دانلود کنسل می شه.
توی کدهایی که به کار بردین در قسمت اول عبارتهست که وقتی اون رو فلس می کنم کاربر با کلیک کردن روی صفحه دانلود رو قطع نمی کنه.
ولی مشکل اینجاست که با روشی نمی تونه دانلود رو قطع کنه.
می خواستم بپرسم ایا روشی وجود داره که یک باتون به دیالوگ دانلود اضافه کنم یا با فشار دادن دکمه ی بک گوشی یک پیام نمیش بده که دانلود قطع بشه یا نه.
نحوه ی استفاده از دکمه ی بک رو بلدم و استفاده از دیالوگ رو هم تا حدودی بلدم.
فقط اگه می شه راهنماییم کنید که چطوری دکمه ی بک رو در کد زیر معرفی کنم تا موقع اجرا شدن کد دانلود (وقتی که کد های صفحه غیر فعال می شوند) اونم اجرا بشه.
برای دانلود هم از کد زیر استفاده کردم
با تشکر از رانمایی های شما
من نرم افزاری دارم طبق آموزش های قبلی شما در اون کدی استفاده کردم که فایل رو دانلود می کنه و در حافظه ی گوشی ذخیره می کنه.
فقط مشکل اینجاست که وقتی کاربر روی صفحه (خارج از دیالوگ بار) کلیک می کنه دانلود کنسل می شه.
توی کدهایی که به کار بردین در قسمت اول عبارت
کد پیاچپی:
mProgressDialog.setCancelable(true);
ولی مشکل اینجاست که با روشی نمی تونه دانلود رو قطع کنه.
می خواستم بپرسم ایا روشی وجود داره که یک باتون به دیالوگ دانلود اضافه کنم یا با فشار دادن دکمه ی بک گوشی یک پیام نمیش بده که دانلود قطع بشه یا نه.
نحوه ی استفاده از دکمه ی بک رو بلدم و استفاده از دیالوگ رو هم تا حدودی بلدم.
فقط اگه می شه راهنماییم کنید که چطوری دکمه ی بک رو در کد زیر معرفی کنم تا موقع اجرا شدن کد دانلود (وقتی که کد های صفحه غیر فعال می شوند) اونم اجرا بشه.
برای دانلود هم از کد زیر استفاده کردم
کد پیاچپی:
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class Subject_2 extends Activity {
public ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subject_2);
Button b00=(Button) findViewById(R.id.button1);
b00.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mProgressDialog = new ProgressDialog(Subject_2.this); // MainActivity = activity name
mProgressDialog.setMessage("در حال دانلود فایل ...");
mProgressDialog.setIndeterminate(true);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
// execute this when the downloader must be fired
final DownloadTask downloadTask = new DownloadTask(Subject_2.this); // MainActivity = activity name
downloadTask.execute("http://uploadboy.com/direct/83dr0064vbhk.html"); // the url to the file you want to download
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
downloadTask.cancel(true);
}
});
}
// usually, subclasses of AsyncTask are declared inside the activity class.
// that way, you can easily modify the UI thread from here
class DownloadTask extends AsyncTask<String, Integer, String> {
private Context context;
public DownloadTask(Context context) {
this.context = context;
}
@Override
protected String doInBackground(String... sUrl) {
// take CPU lock to prevent CPU from going off if the user
// presses the power button during download
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();
// expect HTTP 200 OK, so we don't mistakenly save error report
// instead of the file
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
// this will be useful to display download percentage
// might be -1: server did not report the length
int fileLength = connection.getContentLength();
// download the file
input = connection.getInputStream();
output = new FileOutputStream("/sdcard/kingo root.zip"); // /sdcard/file_name.extension
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
// allow canceling with back button
if (isCancelled())
return null;
total += count;
// publishing the progress....
if (fileLength > 0) // only if total length is known
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);
// if we get here, length is known, now set indeterminate to false
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();
}
}
});
/// end
Button b001=(Button) findViewById(R.id.button2);
b001.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mProgressDialog = new ProgressDialog(Subject_2.this); // MainActivity = activity name
mProgressDialog.setMessage("در حال دانلود فایل ...");
mProgressDialog.setIndeterminate(true);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
// execute this when the downloader must be fired
final DownloadTask downloadTask = new DownloadTask(Subject_2.this); // MainActivity = activity name
downloadTask.execute("http://uploadboy.com/direct/h5t2pwpwtgaz.html"); // the url to the file you want to download
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
downloadTask.cancel(true);
}
});
}
// usually, subclasses of AsyncTask are declared inside the activity class.
// that way, you can easily modify the UI thread from here
class DownloadTask extends AsyncTask<String, Integer, String> {
private Context context;
public DownloadTask(Context context) {
this.context = context;
}
@Override
protected String doInBackground(String... sUrl) {
// take CPU lock to prevent CPU from going off if the user
// presses the power button during download
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();
// expect HTTP 200 OK, so we don't mistakenly save error report
// instead of the file
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK)
return "Server returned HTTP " + connection.getResponseCode()
+ " " + connection.getResponseMessage();
// this will be useful to display download percentage
// might be -1: server did not report the length
int fileLength = connection.getContentLength();
// download the file
input = connection.getInputStream();
output = new FileOutputStream("/sdcard/V root.zip"); // /sdcard/file_name.extension
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
// allow canceling with back button
if (isCancelled())
return null;
total += count;
// publishing the progress....
if (fileLength > 0) // only if total length is known
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);
// if we get here, length is known, now set indeterminate to false
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();
}
}
});
}
}
با تشکر از رانمایی های شما