2016-09-11 16 views
1

を別のclassで分けることはできますか?私はいくつかのclassでそれを再利用したいと思います。 VolleyではStringRequestまたはImageRequestになるようにclassを作成することができます。例えばクラスをダウンロードする適切な方法

public class VolleyStringRequest extends StringRequest{ 

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



    } } 

私はこのlinkと出会いました。すべてが直接宣言されています。 で別途classでこれを達成できますか?

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); 

答えて

0

は、Applicationクラスを拡張グローバルダウンロードクラスを作成し、入力としてURIをとるパブリックメソッドを宣言することができます。

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); 
     } 

     } 

、あなたは、このデータにアクセスする必要があるwhereever、によってApplicationオブジェクトを取得する:あなたのプロジェクトのいくつかの場所でそれを使用することができるように、クラスのdownloadThroughManager(String imageUrl, Context context)メソッド次

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

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

これを私の「Singleton」クラスに含めます。私はそのクラスで 'File'外部をやりたいのです。例:public class DownloadMyManager extends DownloadManager {} – RoCk

1

ラップ。

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

このために 'DownloadManager'を拡張する必要がありますか?私はクラスでそれをラップしたら?同じクラスでは 'File'パスを実行します。あなたは私の質問の考えを持っています。 – RoCk

+1

@RoCkあなたは 'DownloadManager'を拡張する必要はありません。カスタムクラスにこのコードを貼り付け、メソッドに' context'と 'url'を渡すだけです。 –

+0

@RoCkは有用であればこの回答を受け入れます。 –