انجمن سایت کلیدستان


رتبه موضوع:
  • 0 رای - 0 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
قابلیت resume برای دانلود فایل از طریق اینترنت

قابلیت resume برای دانلود فایل از طریق اینترنت

#1
من برای دانلود مثلا یه فایل صوتی از اینترنت از این مشکلات رو دارم :


1- اگه کاربر روی دکمه back کلیک کرد ، دانلود فایل لغو میشه و اگه دوباره بخواد اونو دانلود کنه باید از اول این کار رو انجام بده !


2- و یه مشکل بزرگتر اینه که اگه در حین دانلود صفحه نمایش رو لمس کنه ، بازم دانلود لغو میشه !!!!!!!


دوستان برای حل این مشکل چه راهی رو پیشنهاد میکنند ؟
ممنون



اینم کدی هست که در یه دکمه برای دانلود فایل صوتی استفاده میکنم :

کد پی‌اچ‌پی:
imgdownload.setOnClickListener(new OnClickListener() {
            @
Override
                
public void onClick(View arg0) {
                    
// TODO Auto-generated method stub
             
                     
                 
                 
File file = new File(root.getAbsolutePath() + "/my_folder/" "music.mp3");
                   if (
file.exists() ){
                       
//imgdownload1.setEnabled(false);
                       
imgplay1.setEnabled(true);
                       
Toast.makeText(getApplicationContext(), "فایل قبلا دانلود شده است"Toast.LENGTH_LONG).show();
                   return;   
                   }
                 
                 
                 
                 
                 
                 
 
                    if(
isNetworkConnected()){ // check internet connection
                        // your codes
                         
                     
                   // instantiate it within the onCreate method
                
mProgressDialog = new ProgressDialog(Main.this); // MainActivity = activity name
                
mProgressDialog.setMessage("فایل در حال دانلود می باشد");
                
mProgressDialog.setIndeterminate(true);
                
mProgressDialog.setProgressStyle(ProgressDialog.ST  YLE_HORIZONTAL);
                
mProgressDialog.setCancelable(true);
 
                
// execute this when the downloader must be fired
                
final DownloadTask downloadTask = new DownloadTask(Main.this); // MainActivity = activity name
                
downloadTask.execute("http://آدرس فایل.mp3"); // the url to the file you want to download
                
                
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                    @
Override
                    
public void onCancel(DialogInterface dialog) {
                        
downloadTask.cancel(true);
                        
File file = new File(root.getAbsolutePath() + "/my_folder/" "music.mp3");
                          if (
file.exists() ){
                              
file.delete();
                              return;
                          }
                    }
                });
                 
            }
             
             
            }
             
 
       
// 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<StringIntegerString> {
 
        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 = (PowerManagercontext.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 = (HttpURLConnectionurl.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/my_folder/music.mp3");  //   /sdcard/file_name.extension
                    
File outputFile = new File("/sdcard/my_folder/music.mp3" ".tmp");
                    
output = new FileOutputStream(outputFile);
                     
                     
                    
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(data0count);
                    }
                    
outputFile.renameTo(new File("/sdcard/my_folder/music.mp3"));
                } 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,"Download error: "+resultToast.LENGTH_LONG).show();
            else
                
Toast.makeText(context,"فایل  با موفقیت دانلود شد"Toast.LENGTH_SHORT).show();
             
         
       
          
        }
        
    
     
            }
       }); 
 
پاسخ

قابلیت resume برای دانلود فایل از طریق اینترنت

#2
یکی از دوستان گفتند باید از نخ استفاده کنم یعنی پروسه دانلود رو توی این بزارم :

کد پی‌اچ‌پی:
Thread thread = new Thread(new Runnable() {
 
       @
Override
       
public void run() {
           
//code;
       
}
   }); 

نظر شما چیه ؟
پاسخ

قابلیت resume برای دانلود فایل از طریق اینترنت

#3
سلام. 
این روزها فرصت برای کدنویسی ندارم و معمولا هم عادت دارم که اگر قراره راهنمایی کنم، حتما قبلش خودم کدها رو تست کنم، اما چون این موردی که شما گفتید، بسیار مهم است، سعی می کنم که بدون درگیر شدن در کدنویسی، حداقل روش مناسب برای کدنویسی را برایتان پیدا کنم. (از منابع موجود در وب).
در لینک زیر، چند کد مختلف برای دانلود، ارائه شده است :

www.stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog

 در لینک فوق، در یکی از ارسال ها، 3 روش گفته شده :

کد:
1. Use AsyncTask and show the download progress in a dialog
2. Download from Service
2.1 Use Groundy library
3. Use DownloadManager class (GingerBread and newer only)

خود نویسنده، در آخر ارسالش، نتیجه گرفته است که استفاده از DownloadManager ، بهترین مورد است. علاوه بر این، این عبارت را در انتهای بخش مربوط به DownloadManager نوشته است که بیش از هر چیز برای من مهم بود :

کد:
Download progress will be showing in the notification bar.

که نشان می دهد که میزان پیشرفت دانلود فایل، در notification bar نمایش داده می شود. اگر دقت کرده باشید، در فروشگاه های اندروید که مهمترین وظیفه آنها، دانلود فایل از اینترنت است، دانلود فایل در بخش  notification bar نمایش داده می شود و حتی اگر برنامه آنها را به طور کامل ببندید، باز هم دانلود فایل ادامه دارد.
مثلا برای فروشگاه پارس هاب، هنگام دانلود فایل، میزان پیشرفت دانلود در notification bar نمایش داده می شود :

   

این عکس را زمانی گرفتم که برنامه پارس هاب را به طور کامل بسته بودم، بنابراین فرآیند دانلود فایل، دیگر وابسته به باز یا بسته بودن خود برنامه اصلی نیست.
بنابراین توصیه من این است که به سراغ  DownloadManager  بروید (البته چک کنید که نسخه های اندروید مورد نظر شما را پوشش می دهد یا خیر، منظورم از نظر حداقل نسخه API از اندروید می باشد).

bookbook 
لطفا برای درج کد، از دکمه مخصوص درج کد در ادیتور انجمن استفاده کنید.
در مورد برنامه نویسی، مدیران تنها راهنمایی می کنند و نوشتن برنامه نهایی، به عهده کاربران می باشد (اینجا محلی برای یادگیری است، نه سفارش کدنویسی).
کاربران باید ابتدا خود به خطایابی برنامه بپردازند، نه اینکه به محض دیدن خطا، کدها را در انجمن، copy و paste کرده و از مدیران انتظار بررسی داشته باشند.
پاسخ
 سپاس شده توسط moralschool ، zanyyy ، شماره مجازی امارات ، تلگرام ضد فیلتر 2023


پرش به انجمن:


کاربران در حال بازدید این موضوع: 1 مهمان