Попытка добавить Notification Builder в AsyncTask - PullRequest
0 голосов
/ 01 марта 2020

Кто-то мне помогает Pls

Я хотел реализовать построитель уведомлений в моем методе AsyncTask для загрузки файлов с сервера. Уведомление, которое содержит индикатор выполнения с увеличивающейся базой на длину файла, показывает процент загрузки и сколько МБ осталось, с паузой и возобновлением, я пробую это, но уведомление все еще не отображается, пока загрузка выполняется в фоновом режиме, есть мой код:

class DownloadFileFromURL extends AsyncTask<String, String, String> {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            showDialog(progress_bar_type);
        }


        @Override
        protected String doInBackground(String... f_url) {
            int count;
            try {
                URL url = new URL(f_url[0]);
                URLConnection conection = url.openConnection();
                conection.connect();
                int lenghtOfFile = conection.getContentLength();
                InputStream input = new BufferedInputStream(url.openStream(), 8192);

                String folder = "/data/data/" + getPackageName() + "/files/";

                File directory = new File(folder);


                if (!directory.exists()) {
                    directory.mkdirs();
                }

                OutputStream output = new FileOutputStream(folder + getIntent().getStringExtra("title") + ".mp4");

                byte data[] = new byte[1024];

                long total = 0;

                while ((count = input.read(data)) != -1) {
                    total += count;
                    publishProgress(""+(int)((total*100)/lenghtOfFile));
                    output.write(data, 0, count);
                }
                output.flush();
                output.close();
                input.close();

            } catch (Exception e) {
                Log.e("Error: ", e.getMessage());
            }

            return null;
        }


        protected void onProgressUpdate(String... progress) {
            pDialog.setProgress(Integer.parseInt(progress[0]));
        }

        @Override
        protected void onPostExecute(String file_url) {
            dismissDialog(progress_bar_type);
            String type = "downloads";
            Update(type);

            View layout = getLayoutInflater().inflate(R.layout.toast_custom, findViewById(R.id.custom_toast_layout_id));
            TextView text = layout.findViewById(R.id.text);
            text.setText(getIntent().getStringExtra("title") + "  - Downloaded Successfully!");
            Toast toast = new Toast(getApplicationContext());
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();

        }



    }

1 Ответ

0 голосов
/ 01 марта 2020

Где код для отображения уведомлений и какую android версию os вы используете?

...