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

标签 java android class android-download-manager

我可以将 DownloadManager 分离到一个单独的 class 中吗? 我想在我的多个 class 中重复使用它。在 Volley 中,我们可以创建一个 class 扩展 StringRequestImageRequest

例如:

public class VolleyStringRequest extends StringRequest{

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



    } }

我遇到了这个 link .所有这些都是直接声明的。 如何通过扩展 DownloadManager 在单独的 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);

最佳答案

将以下 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);
        }

关于java - 分离类的正确方法 - 下载管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39434742/

相关文章:

java - 将变量从一个类传递到另一个类

java - 包含对象的节点的堆栈实现

java - 重新加载页面后 RFT 找不到 TestObjects

java - 使用 WifiManager 连接到无线网络 (wpa2)

java - Java 中非实例化类的内存管理是如何工作的?

python - python类/子类继承背后的基本原理

java - Spring自动类型转换不适用于字符串到日期

java - 如何用java从数据库中获取所有子类别

java - 升级到 Facebook SDK 12.0.0(或 13)后,Android CallbackManager 回调停止工作

android - 我可以在 Android 1.5 中运行 Android 2.0 应用程序吗?