2011-05-04 13 views
7

मैं एक यूआरएल से एक बाइनरी फ़ाइल डाउनलोड करना चाहता हूं। क्या एंड्रॉइड डाउनलोड मैनेजर क्लास का उपयोग करना संभव है जो मुझे DownloadManager class मिला?एंड्रॉइड: डाउनलोड मैनेजर क्लास का उपयोग कैसे करें?

+0

चेक बाहर इस ट्यूटोरियल http://www.gadgetsaint.com/android/download-manager/ – ASP

उत्तर

26

यह संभव, एंड्रॉयड डाउनलोड प्रबंधक वर्ग है कि मैं यहाँ

हाँ पाया उपयोग करने के लिए है कि हालांकि बाद में Android API स्तर 9 (संस्करण 2.3) ही उपलब्ध है है। Here is a sample projectDownloadManager का उपयोग प्रदर्शित करता है।

+1

@ CommonsWare..I अपने सभी कोड सब के सब महान हैं के माध्यम से चला गया। मैं वाईफाई के माध्यम से DownloadManager का उपयोग करना चाहता हूं .. मुझे एसएफटीपी का उपयोग करना है और राउटर से फ़ाइल डाउनलोड करना है .. क्या यह संभव है? मुझे इसके लिए मार्गदर्शन करेगा .. – NovusMobile

+2

@ सैटियम: 'डाउनलोड प्रबंधक' एसएफटीपी का समर्थन नहीं करता है। एंड्रॉइड में कुछ भी एसएफटीपी AFAIK का समर्थन करता है। इसके लिए आपको एक थर्ड-पार्टी जेएआर ढूंढना होगा। – CommonsWare

+0

मैंने पाया कि एपीआई जिसे मैंने जावा नाम "जेएससीएच" में प्रयोग किया था। @ कॉमन्सवेयर मेरे जावा प्रोजेक्ट में यह सही है लेकिन यहां एंड्रॉइड में नहीं है .. क्या यह संभव है? या मुझे अन्य समस्या का सामना करना पड़ सकता है? कृपया उत्तर दें .. – NovusMobile

6

उपयोग DownloadManager वर्ग (Gingerbread और नए केवल)

जिंजरब्रेड के लिए एक नई सुविधा, DownloadManager है, जो व्यवस्था करने के लिए आदि, आप आसानी से फ़ाइलों को डाउनलोड और धागे से निपटने की कड़ी मेहनत को सौंपने के लिए अनुमति देता है धाराओं, ले आया।

पहले, आइए एक उपयोगिता विधि देखते हैं:

/** 
* @param context used to check the device version and DownloadManager information 
* @return true if the download manager is available 
*/ 
public static boolean isDownloadManagerAvailable(Context context) { 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { 
     return true; 
    } 
    return false; 
} 

विधि का नाम यह सब बताते हैं। एक बार जब आप यकीन है कि DownloadManager उपलब्ध है कर रहे हैं, तो आप कुछ इस तरह कर सकते हैं:

String url = "url you want to download"; 
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); 
request.setDescription("Some descrition"); 
request.setTitle("Some title"); 
// in order for this if to run, you must use the android 3.2 to compile your app 
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
    request.allowScanningByMediaScanner(); 
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
} 
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "name-of-the-file.ext"); 

// get download service and enqueue file 
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
manager.enqueue(request); 

डाउनलोड प्रगति सूचना पट्टी में दिखा दिया जाएगा।

3
DownloadManager downloadmanager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); 
Uri uri = Uri.parse("http://www.example.com/myfile.mp3"); 
DownloadManager.Request request = new DownloadManager.Request(uri); 
request.setTitle("My File"); 
request.setDescription("Downloading"); 
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 
request.setDestinationUri(Uri.parse("file://" + folderName + "/myfile.mp3")); 
downloadmanager.enqueue(request); 
संबंधित मुद्दे