即使应用终止,如何在后台使用下载管理器下载多个文件?

问题描述 投票:0回答:1

我正在实施一个项目,用户可以通过在对话框中选择(从+到+)来下载简短的mp3,所有声音都下载为一个(文件数可以达到300个文件,每个文件大小约为100kb)。

我设法使所有工作顺利进行。.已成功创建带有应用程序名称的文件夹,已成功在App文件夹内创建带有引用者名称的子文件夹,并以正确的名称下载文件并将其保存在正确的子文件夹中,可应要求播放mp3文件。

我面临的唯一问题是下载通知(即使同时成功下载了所有文件,多个通知也会同时运行并显示永远下载。

这是我的完整代码:

 private void createDirectory() {
        String folderName = getResources().getString(R.string.app_name);
        File file = new File(Environment.getExternalStorageDirectory() + "/" + folderName);
        if (!file.exists()) {
            file.mkdir();
            Log.w("Folder ...", folderName + " directory has been created");
        } else {
            Log.w("Folder ...", folderName + " folder already exists");
        }
        createSubDirectory();
    }


    private void createSubDirectory() {
        String subfolderName = getResources().getString(R.string.app_name) + "/" + Sreciter;
        File file = new File(Environment.getExternalStorageDirectory() + "/" + subfolderName);
        if (!file.exists()) {
            file.mkdir();
            Log.w("Folder ...", subfolderName + " Subdirectory has been created");
        } else {
            Log.w("Folder ...", subfolderName + " folder already exists");
        }
 prepareDownload();
    }

    private void prepareDownload() {
        DatabaseAccess databaseAccess = DatabaseAccess.getInstance(Settings.this);
        int from = Integer.valueOf(Sfrom) + 1;
        int to = Integer.valueOf(Sto) + 2;
        Log.w("Values", "from " + from + " to " + to);

        list_ids.clear();
        list_urls.clear();
        list_paths.clear();

        databaseAccess.open();
        for (int i = from; i < to; i++) {
            Ayah_download_id = String.valueOf(databaseAccess.getAudioID(Ssorah, String.valueOf(i)));
            url = "https://alitkaan.com/waratil/" + reciterName + "/" + Ayah_download_id + ".mp3";
            storeLPath = Environment.getExternalStorageDirectory() + "/" + getResources().getString(R.string.app_name) + "/" + Sreciter;
            File optputfile = new File(storeLPath, Ayah_download_id + ".mp3");

            if (!optputfile.exists()) {
                list_ids.add(Ayah_download_id);
                list_urls.add(url);
                list_paths.add(storeLPath);
            } else {
                Log.w("file", "exists");
            }
        }
        databaseAccess.close();

        File basmalah = new File(storeLPath, "001001.mp3");
        if(!basmalah.exists()){
            list_ids.add("001001");
            list_urls.add("https://alitkaan.com/waratil/" + reciterName + "/001001.mp3");
            list_paths.add(storeLPath);
        }

        if (list_ids.size() < 1) {
            Utils.CustomToast(Settings.this, "تم تحميل هذه الملفات مسبقًا.");
        } else {
            downloadAsyncTask task = new downloadAsyncTask(this);
            task.execute();
        }

    }

    private boolean CNCS() {
        boolean wifiConnected = false;
        boolean mobileConnected = false;
        boolean net_available = false;

        ConnectivityManager connMang = (ConnectivityManager)
                getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeInfo = connMang.getActiveNetworkInfo();
        if (activeInfo != null && activeInfo.isConnected()) {
            wifiConnected = activeInfo.getType() == ConnectivityManager.TYPE_WIFI;
            mobileConnected = activeInfo.getType() == ConnectivityManager.TYPE_MOBILE;
            if (wifiConnected) {
                ConnectionType = "WIFI";
                net_available = true;
            } else if (mobileConnected) {
                ConnectionType = "Mobile";
                net_available = true;
            }
        } else {
            ConnectionType = "No Connection";
            net_available = false;
        }
        return net_available;
    }

    private static class downloadAsyncTask extends AsyncTask<Void, Void, String> {
        private WeakReference<Settings> settingsWeakReference;

        downloadAsyncTask(Settings activity) {
            settingsWeakReference = new WeakReference<Settings>(activity);
        }


        @Override
        protected String doInBackground(Void... voids) {
            Settings activity = settingsWeakReference.get();
            DownloadManager manager = (DownloadManager) activity.getSystemService(Context.DOWNLOAD_SERVICE);
            long requestId;

            if (activity == null || activity.isFinishing()) {
                return "لم يتم التحميل";
            }
            for (int y = 0; y < activity.list_ids.size(); y++) {
                String d_id = activity.list_ids.get(y);
                String d_url = activity.list_urls.get(y);
                String d_path = activity.list_paths.get(y);

                int count = y + 1;

                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(d_url));
                request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
                request.setAllowedOverRoaming(false);
                request.setTitle(" تحميل " + activity.list_ids.size() + "/" + count);
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
                request.setDestinationUri(Uri.fromFile(new File(d_path, d_id + ".mp3")));

                requestId = manager.enqueue(request);

                if (count == activity.list_ids.size()) {
                    activity.last_downloaded = requestId;
                }

                Log.w("Downloaded .. ", d_id + ".mp3");
            }
            return "جاري التحميل ...";
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Settings activity = settingsWeakReference.get();
            if (activity == null || activity.isFinishing()) {
                return;
            }
            if (!activity.CNCS()) {
                Utils.CustomToast(activity, "لا يوجد اتصال بالنت .. سيتم تحميل الملفات تلقائياً عند الاتصال بالنت");
            } else {
                Utils.CustomToast(activity, s);
            }
        }
    }

作为解决方案,我已经隐藏了下载通知,但这不是我想要的。.我只想显示(一个)通知,其中包含要下载的文件总数作为标题,而文件名作为标题下载。说明。

请帮助!

java android-studio download android-download-manager
1个回答
0
投票

答案比我还简单...

© www.soinside.com 2019 - 2024. All rights reserved.