通知正在放缓

时间:2017-01-23 11:27:40

标签: android android-intent service notifications

在我的应用程序中,我使用意向服务下载文件。下载开始时,还会创建下载进度通知。一切正常,但问题是通知减慢了手机:以下是我的代码: 注意:我删除了代码中不相关的部分,以简单明了:

public void startDownload(String name, String packageName, String path, boolean obb, boolean data) {
    try {
        notificationId = generateNoficationId(packageName);
        Intent i = new Intent(getApplicationContext(), DownloadListView.class);
        PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);

            mBuilder = new NotificationCompat.Builder(this);
            mBuilder.setContentTitle(name)
                    .setContentText("downloading...")
                    .setSmallIcon(R.drawable.newiconpurple)
                    .setContentInfo("0%")
                    .setProgress(100, 0, true)
                    .setContentIntent(pi)
                    .setOngoing(true)
                    .setAutoCancel(true);

            nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notification = mBuilder.build();
            startForeground(notificationId, notification);

            url = new URL(IPClass.SERVERIP + path + "/" + packageName);
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setReadTimeout(7000);
            long fileLength = connection.getContentLength();
            connection.connect();
            input = connection.getInputStream();
            output = new FileOutputStream("/sdcard/downloadtmp/" + packageName, true);
            byte dataSize[] = new byte[16384];
            int count;
            downloadedSoFar = 0;
            continueDownload = true;
            while ((count = input.read(dataSize)) > 0 && continueDownload) {
                if (!downloadCancelList.contains(packageName)) {
                    downloadedSoFar = downloadedSoFar + count;
                    output.write(dataSize, 0, count);
                    progress = (int) ((downloadedSoFar * 100L) / fileLength);
                    progressChange(name, progress, packageName, obb, data);
                } else {
                    continueDownload = false;
                    int startId = downloadIdList.get(packageName);
                    stopSelf(startId);
                    nm.cancel(notificationId);
                    downloadCancelList.remove(packageName);
                    stopForeground(true);
                }
                Thread.sleep(250);
            }
        }
    } catch (Exception ex) {
        nm.cancel(notificationId);
    } finally {
        try {
            if (output != null) output.close();
            if (input != null) input.close();
        } catch (IOException ignored) {

        }
        if (connection != null) {
            connection.disconnect();
        } else if (resumeConnection != null) {
            resumeConnection.disconnect();
        }
    }
}

以下是progressChange方法:

   void progressChange(final String name, final int progress, final String packageName, boolean obb, boolean data) {

        if (lastupdate != progress) {
            lastupdate = progress;
            if (progress < 100) {

                mBuilder.setProgress(100, Integer.valueOf(progress), false).setContentInfo(progress + "%");
                nm.notify(notificationId, mBuilder.build());

            } else if (progress == 100) {
                stopForeground(true);
                mBuilder.setContentText("download finished").setProgress(0, 0, false).setOngoing(false).setContentInfo("");
                nm.notify(notificationId, mBuilder.build());
                new DownloadController().removeFromDownloadList(packageName);
                new DownloadFinished(name, packageName, obb, data).execute();
            }
        }
    }

请帮助我!

1 个答案:

答案 0 :(得分:0)

多次发送通知真是个坏主意。每个通知都需要膨胀,存储和显示。从屏幕上删除时,通知内容不会立即从内存中删除。您只需创建占用RAM的大量视图。