2016-09-11 4 views
1

Kann ich DownloadManager in einem separaten class trennen? Ich möchte es in meinen verschiedenen class wiederverwendet werden. In Volley können wir eine class Verlängerung StringRequest oder ImageRequest machen.Richtiger Weg zum Trennen der Klasse - Download Manager

Zum Beispiel:

public class VolleyStringRequest extends StringRequest{ 

public VolleyStringRequest(Response.Listenter<String> listener, 
          Response.ErrorListener errorListener) 
    { 
     super(Method.POST, " " , listener, errorListener); 



    } } 

kam ich mit diesem link über. Alles ist direkt deklarieren. Wie erreiche ich dies in einem separaten class von erweitert DownloadManager?

DownloadManager dm= (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 
DownloadManager.Request request = new DownloadManager.Request(Uri.parse("https://www.google.com.tw/images/srpr/logo4w.png")); 
dm.enqueue(request); 

DownloadManager dm= (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 
DownloadManager.Request request = new DownloadManager.Request(Uri.parse("https://www.google.com.tw/images/srpr/logo4w.png")); 
dm.enqueue(request); 

Antwort

0

Sie können eine globale Download-Klasse erstellen, die eine öffentliche Methode Application-Klasse und erklären erstreckt, die die Uri als Eingabe verwendet.

class DownloadClass extends Application { 

     public void startDownload(String url) 
     { 
      DownloadManager dm= (DownloadManager) getSystemService(DOWNLOAD_SERVICE); 
      DownloadManager.Request request = new   
      DownloadManager.Request(Uri.parse(url)); 
      dm.enqueue(request); 
     } 

     } 

dann wo auch immer Sie brauchen diese Daten zugreifen, die von Application-Objekt erhalten:

DownloadClass download=(DownloadClass)context.getApplication(); 

download.startDownload("https://www.google.com.tw/images/srpr/logo4w.png"); 
+0

Ich werde dies in meine 'Singleton'-Klasse aufnehmen. Die Sache ist, ich möchte die 'Datei' external mit dieser Klasse machen. Beispiel: öffentliche Klasse DownloadMyManager erweitert den DownloadManager {} – RoCk

1

Wrap folgenden downloadThroughManager(String imageUrl, Context context) Methode in der Klasse, so dass Sie es an mehreren Stellen in Ihrem Projekt verwenden können.

public static void downloadThroughManager(String imageUrl, Context context) { 


        File path = new File(imageUrl); 
        String fileName = path.getName(); 
        final DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); 
        Uri uri = Uri.parse(imageUrl); 
        DownloadManager.Request request = new DownloadManager.Request(uri); 
        request.setTitle(fileName); 
        request.setDescription(fileName); 
        request.setVisibleInDownloadsUi(true); 
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); 

        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName); 
        long ref = downloadManager.enqueue(request); 

        IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE); 




     final BroadcastReceiver receiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       long downloadReference = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1); 
       Log.i("GenerateTurePDfAsync", "Download completed"); 


       DownloadManager.Query query = new DownloadManager.Query(); 
       query.setFilterById(downloadReference); 

       Cursor cur = downloadManager.query(query); 

       if (cur.moveToFirst()) { 
        int columnIndex = cur.getColumnIndex(DownloadManager.COLUMN_STATUS); 



        if (DownloadManager.STATUS_SUCCESSFUL == cur.getInt(columnIndex)) { 
         String uriString = cur.getString(cur.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)); 

         Toast.makeText(context, "File has been downloaded successfully.", Toast.LENGTH_SHORT).show(); 


        } else if (DownloadManager.STATUS_FAILED == cur.getInt(columnIndex)) { 
         int columnReason = cur.getColumnIndex(DownloadManager.COLUMN_REASON); 
         int reason = cur.getInt(columnReason); 
         switch(reason){ 

          case DownloadManager.ERROR_FILE_ERROR: 
           Toast.makeText(context, "Download Failed.File is corrupt.", Toast.LENGTH_LONG).show(); 
           break; 
          case DownloadManager.ERROR_HTTP_DATA_ERROR: 
           Toast.makeText(context, "Download Failed.Http Error Found.", Toast.LENGTH_LONG).show(); 
           break; 
          case DownloadManager.ERROR_INSUFFICIENT_SPACE: 
           Toast.makeText(context, "Download Failed due to insufficient space in internal storage", Toast.LENGTH_LONG).show(); 
           break; 

          case DownloadManager.ERROR_UNHANDLED_HTTP_CODE: 
           Toast.makeText(context, "Download Failed. Http Code Error Found.", Toast.LENGTH_LONG).show(); 
           break; 
          case DownloadManager.ERROR_UNKNOWN: 
           Toast.makeText(context, "Download Failed.", Toast.LENGTH_LONG).show(); 
           break; 
          case DownloadManager.ERROR_CANNOT_RESUME: 
           Toast.makeText(context, "ERROR_CANNOT_RESUME", Toast.LENGTH_LONG).show(); 
           break; 
          case DownloadManager.ERROR_TOO_MANY_REDIRECTS: 
           Toast.makeText(context, "ERROR_TOO_MANY_REDIRECTS", Toast.LENGTH_LONG).show(); 
           break; 
          case DownloadManager.ERROR_DEVICE_NOT_FOUND: 
           Toast.makeText(context, "ERROR_DEVICE_NOT_FOUND", Toast.LENGTH_LONG).show(); 
           break; 

         } 
        } 
       } 
      } 

     }; 


     context.registerReceiver(receiver, filter); 
     } 
+0

Muss ich den '' '' '' DownloadManager ''dafür erweitern? Wenn ich es mit einer Klasse verpacke? Und in der gleichen Klasse mache ich den 'File' Pfad. Du hast meine Frage Idee. – RoCk

+1

@RoCk Sie müssen nicht 'DownloadManager' erweitern, fügen Sie einfach diesen Code in eine benutzerdefinierte Klasse ein und übergeben Sie' context' und 'url' in der Methode. –

+0

@RoCk Akzeptieren Sie diese Antwort, wenn es nützlich ist. –

Verwandte Themen