Java 分离类的正确方法-下载管理器

Java 分离类的正确方法-下载管理器,java,android,class,android-download-manager,Java,Android,Class,Android Download Manager,我可以在单独的类中分离DownloadManager吗?我想在我的几个类中重复使用它。在Volley中,我们可以创建一个class扩展StringRequest或ImageRequest 例如: public class VolleyStringRequest extends StringRequest{ public VolleyStringRequest(Response.Listenter<String> listener,

我可以在单独的
类中分离
DownloadManager
吗?
我想在我的几个
类中重复使用它。在
Volley
中,我们可以创建一个
class
扩展
StringRequest
ImageRequest

例如:

public class VolleyStringRequest extends StringRequest{

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



    } }

您可以创建一个扩展应用程序类的全局下载类,并声明一个将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);
       }

      }
然后,无论何时需要访问此数据,都可以通过以下方式获取应用程序对象:

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

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

在类中包装以下
downloadThroughManager(String imageUrl,Context Context)
方法,以便您可以在项目中的多个位置使用它。

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

我将把它包括在我的
Singleton
类中。问题是我想对该类执行
文件
外部操作。例如:公共类DownloadMyManager扩展DownloadManager{}为此,我需要
扩展DownloadManager
?如果我用一个类来包装它?在同一个类中,我将执行
文件
路径。你有了“我的问题”的想法。@RoCk你不需要扩展
DownloadManager
,只需将此代码粘贴到任何自定义类中,并在方法中传递
context
url
 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);
        }