Android - загрузка файла + уведомление в строке состояния о замедлении работы телефона - PullRequest
5 голосов
/ 03 августа 2011

В настоящее время у меня есть asynctask, который загружает mp3 с сервера.Когда пользователь начинает загружать его, создается уведомление в строке состояния.Это отображает прогресс загрузки в режиме реального времени.Единственное, что меня беспокоит, это то, что телефон тормозит почти слишком сильно.Есть ли способ задержать отображаемый прогресс или способ сделать мой код быстрее?Спасибо.

Код ниже:

public class DownloadFile extends AsyncTask<String, String, String> {
    CharSequence contentText;
    Context context;
    CharSequence contentTitle;
    PendingIntent contentIntent;
    int HELLO_ID = 1;
    long time;
    int icon;
    CharSequence tickerText;
    File file;

    public void downloadNotification() {
        String ns = Context.NOTIFICATION_SERVICE;
        notificationManager = (NotificationManager) getSystemService(ns);

        icon = R.drawable.sdricontest;
        //the text that appears first on the status bar
        tickerText = "Downloading...";
        time = System.currentTimeMillis();

        notification = new Notification(icon, tickerText, time);

        context = getApplicationContext();
        //the bold font
        contentTitle = "Your download is in progress";
        //the text that needs to change
        contentText = "0% complete";
        Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
        notificationIntent.setType("audio/*");
        contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        notificationManager.notify(HELLO_ID, notification);
    }

    @Override
    protected void onPreExecute() {
        //execute the status bar notification
        downloadNotification();
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... url) {
        int count;
        try {
            URL url2 = new URL(sdrUrl);
            HttpURLConnection connection = (HttpURLConnection) url2.openConnection();
            connection.setRequestMethod("GET");
            connection.setDoOutput(true);
            connection.connect();

            int lengthOfFile = connection.getContentLength();

            //make the stop drop rave folder
            File sdrFolder = new File(Environment.getExternalStorageDirectory() + "/StopDropRave");
            boolean success = false;

            if (!sdrFolder.exists()) {
                success = sdrFolder.mkdir();
            }
            if (!success) {
                String PATH = Environment.getExternalStorageDirectory()
                        + "/StopDropRave/";
                file = new File(PATH);
                file.mkdirs();
            } else {
                String PATH = Environment.getExternalStorageDirectory()
                        + "/StopDropRave/";
                file = new File(PATH);
                file.mkdirs();
            }

            String[] path = url2.getPath().split("/");
            String mp3 = path[path.length - 1];
            String mp31 = mp3.replace("%20", " ");
            String sdrMp3 = mp31.replace("%28", "(");
            String sdrMp31 = sdrMp3.replace("%29", ")");
            String sdrMp32 = sdrMp31.replace("%27", "'");

            File outputFile = new File(file, sdrMp32);
            FileOutputStream fos = new FileOutputStream(outputFile);

            InputStream input = connection.getInputStream();

            byte[] data = new byte[1024];
            long total = 0;
            while ((count = input.read(data)) != -1) {
                total += count;
                publishProgress("" + (int) (total * 100 / lengthOfFile));
                fos.write(data, 0, count);
            }
            fos.close();
            input.close();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void onProgressUpdate(String... progress) {
        contentText = Integer.parseInt(progress[0]) + "% complete";
        notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
        notificationManager.notify(HELLO_ID, notification);
        super.onProgressUpdate(progress);
    }
}

Ответы [ 4 ]

5 голосов
/ 03 августа 2011

Я видел похожие результаты, вам не нужно нажимать на обновление уведомление так часто, я изменил мой, чтобы обновить только несколько раз в секунду.(Например, в onProgressUpdate отслеживайте последний раз, когда вы вызывали notify, и уведомляйте call только в том случае, если вы прошли 100 мс предыдущего вызова или если вы используете максимальное значение.

4 голосов
/ 24 сентября 2015

Однажды у меня была похожая проблема, я решил ее, используя CountDownTimer .

Как и в случае с @superfell, вы можете регулярно вызывать обновление AsyncTask при загрузке файла.И вызывайте диспетчер уведомлений только через определенный интервал.

После вызова start() CountDownTimer он будет вызывать функцию onTick() через каждый фиксированный интервал времени и будет вызывать onFinish() либо по истечении таймераили когда вызывается явно.Функция cancel() отменяет только таймер и не вызывает метод onFinish().

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

    CountDownTimer cdt;
    int id = i;
    NotificationManager mNotifyManager;
    NotificationCompat.Builder mBuilder;

    @Override
    protected void onPreExecute() {
        /**
         * Create custom Count Down Timer
         */
        cdt = new CountDownTimer(100 * 60 * 1000, 500) {
            public void onTick(long millisUntilFinished) {
                mNotifyManager.notify(id, mBuilder.build());
            }

            public void onFinish() {
                mNotifyManager.notify(id, mBuilder.build());
            }
        };
    }

    @Override
    protected String doInBackground(String... strings) {
        /**
         * Start timer to update Notification
         * Set Progress to 20 after connection
         * Build Notification
         * Increment Progress
         * Download and Save file
         */
        try {
            mNotifyManager =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            mBuilder = new NotificationCompat.Builder(context);
            mBuilder.setContentTitle("Downloading File")
                    .setContentText(file_name)
                    .setProgress(0, 100, false)
                    .setOngoing(true)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setPriority(Notification.PRIORITY_LOW);

            // Initialize Objects here
            publishProgress("5");
            mNotifyManager.notify(id, mBuilder.build());
            cdt.start();

            // Create connection here
            publishProgress("20");

            // Download file here
            while ((count = input.read(data)) != -1) {
                total += count;
                publishProgress("" + (int) (20 + (total * 80 / fileLength)));
                output.write(data, 0, count);
            }
        } catch (Exception e) {
            return "Failed";
        }
        return "Success";
    }

    @Override
    protected void onProgressUpdate(String... values) {
        /**
         * Update Download Progress
         */
        mBuilder.setContentInfo(values[0] + "%")
                .setProgress(100, Integer.parseInt(values[0]), false);
    }

    @Override
    protected void onPostExecute(String s) {

        String title;
        if (s.equals("Success")) {
            title = "Downloaded";
        } else {
            title = "Error Occurred";
        }
        mBuilder.setContentTitle(title)
                .setContentInfo("")
                .setOngoing(false)
                .setProgress(0, 0, false);
        cdt.onFinish();
        cdt.cancel();
    }
}

Рекомендуется сначала позвонить onFinish(), а затем позвонить cancel().

2 голосов
/ 15 июля 2016

Я тоже испытывал эту проблему.Я обновлял индикатор прогресса WAY слишком часто (даже если прогресс не изменился), вот как я это исправил:

        // While loop from generic download method.
        int previousProgress = 0;
        while ((count = inputStream.read(buff)) != -1) {
            outputStream.write(buff, 0, count);
            totalBytesDownloaded += count;
            int prog = (int) (totalBytesDownloaded * 100 / contentLength);
            if (prog > previousProgress) {
                // Only post progress event if we've made progress.
                previousProgress = prog;
                myPostProgressMethod(prog);

            }
        }

Теперь приложение работает отлично, а пользователь по-прежнемуполучает уведомление о прогрессе.

0 голосов
/ 09 декабря 2014

У меня была та же проблема, что я не смог обновить уведомление индикатора выполнения даже с интервалом в 3 секунды, поэтому после часов копания я осознал тот факт, что всякий раз, когда мы обновляем уведомление, объект RemoteView должен быть повторносоздается и повторно инициализируется в contentView объекта Notification.После этого я смог обновить индикатор выполнения уведомлений с интервалом 100-500 мс в течение очень длительного периода, не сталкиваясь с блокировкой пользовательского интерфейса.

Примечание. Если вы не согласны, вы можете проверитьэтот ответ, запустив этот фрагмент после комментирования отмеченной строки и увидеть разницу.Это может занять около 5 минут, чтобы начать серьезную блокировку пользовательского интерфейса, которая нагревает ваше устройство и может перестать функционировать.Я попытался с S3 mini с Android 4.2.2, и метод updateNotification (....) был вызван из рабочего потока внутри службы.Более того, я уже дважды проверил его и не знаю, что происходит, когда Notification.Builder используется для той же цели.

Примечание. Причина написания этого ответа после 3 лет вопроса заключается в том, чтоИнтересно, что я не нашел ни одного ответа на стеке или другого сообщения в блоге, где рассматривается эта серьезная проблема с помощью этого очень простого решения.

Я надеюсь, что этот ответ будет полезен для других новичков, таких как я.Наслаждайтесь.

Вот мой скопированный код, который вы можете использовать напрямую .... Я использую тот же код для обновления макета уведомлений, который содержит два ProgressBars и четыре TextView с частотой 500 мс-100 мс.

//long mMaxtTimeoutNanos = 1000000000 // 1000ms.
long mMinTimeNanos     = 100000000;//100ms minimum update limit. For fast downloads.
long mMaxtTimeoutNanos = 500000000;//500ms maximum update limit. For Slow downloads
long mLastTimeNanos = 0;
private void updateNotification(.....){
    // Max Limit
    if (mUpdateNotification || ((System.nanoTime()-mLastTimeNanos) > mMaxtTimeoutNanos)) {
        // Min Limit
        if (((System.nanoTime() - mLastTimeNanos) > mMinTimeNanos)) {
            mLastTimeNanos = System.nanoTime();
            // instantiate new RemoteViews object.
            // (comment out this line and instantiate somewhere
            // to verify that the above told answer is true)
            mRemoteView = new RemoteViews(getPackageName(),
                    R.layout.downloader_notification_layout);
            // Upate mRemoteView with changed data
            ...
            ...
            // Initialize the already existing Notification contentView
            // object with newly instatiated mRemoteView.
            mNotification.contentView = mRemoteView;
            mNotificationManager.notify(mNotificatoinId, mNotification);
            mUpdateNotification = false;
        }
    }
}
...