简体   繁体   English

如何创建一个自动更新通知,显示文件复制进度没有滞后?

[英]How to create an auto-updating notification showing files copying progress without lags?

I need to show the progress of the file copying process using the notification. 我需要使用通知显示文件复制过程的进度。 I solved the problem but my phone was laging during the process. 我解决了这个问题,但我的手机在这个过程中一直在肆虐。 So how can I get rid of the lags? 那我怎么能摆脱滞后?

I've tried to solve this problem using AsyncTask<...>. 我试图使用AsyncTask <...>来解决这个问题。 And if I tried to move the notification in the notifications panel left or right, it was moving very slowly. 如果我尝试在通知面板中向左或向右移动通知,则移动速度非常慢。 As the copying process was finished, the notification started behaving as a usual notification without lags. 随着复制过程的完成,通知开始表现为通常的通知,没有滞后。

To start copy I used 我开始使用副本

new notification().execute(myFile, null, null);

The copying class: 复制课:

public class notification extends AsyncTask<File, Integer, Void> {
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mContext);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, "CopyMoveNotification");
    int PROGRESS_MAX = 100;
    int CUR_PROGRESS = 0;
    public void onPreExecute(){
        builder.setContentTitle("Files copying")
                .setContentText("Copying in progress")
                .setSmallIcon(R.drawable.ic_notification_copy)
                .setPriority(NotificationCompat.PRIORITY_MAX);
        builder.setProgress(PROGRESS_MAX, CUR_PROGRESS, false);
        builder.setOngoing(true);
        notificationManager.notify(1, builder.build());
    }
    @Override
    protected Void doInBackground(File... files) {
        String path = mCurPath;
        File file = new File(path+File.separator+files[0].getName());
        try (InputStream in = new FileInputStream(files[0])) {
            try (OutputStream out = new FileOutputStream(path+File.separator+files[0].getName())) {
                byte[] buf = new byte[8192];
                int len;
                while ((len = in.read(buf)) > 0) {
                    CUR_PROGRESS = Math.round(100 * file.length()/files[0].length());
                    publishProgress(CUR_PROGRESS);
                    out.write(buf, 0, len);

                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onProgressUpdate(Integer... progress){
        builder.setProgress(PROGRESS_MAX, CUR_PROGRESS, false);
        notificationManager.notify(1, builder.build());
    }
}
public class notification extends AsyncTask<File, Integer, Void> {
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mContext);
    int PROGRESS_MAX = 100;
    private String ChannelId = "CopyMoveNotification";
    private Notification notification;

    public void onPreExecute() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, ChannelId);
        builder.setContentTitle("Files copying")
                .setContentText("Copying in progress")
                .setSmallIcon(R.drawable.ic_notification_copy)
                .setPriority(NotificationCompat.PRIORITY_MAX);
        builder.setProgress(PROGRESS_MAX, 0, false);
        builder.setOngoing(true);
        notification = builder.build();
        notificationManager.notify(1, notification);
        createChannelIfNeeded();
    }

    void createChannelIfNeeded() {
        if (Build.VERSION.SDK_INT >= 26) {
            NotificationChannel channel = new NotificationChannel(ChannelId,
                    "Files copying", NotificationManager.IMPORTANCE_LOW);
            channel.setDescription("Copying in progress");
            channel.enableLights(false);
            channel.enableVibration(false);
            NotificationManager manager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
            if (manager != null) {
                manager.createNotificationChannel(channel);
            }
        }
    }

    @Override
    protected Void doInBackground(File... files) {
        String path = mCurPath;
        File file = new File(path + File.separator + files[0].getName());
        try (InputStream in = new FileInputStream(files[0])) {
            try (OutputStream out = new FileOutputStream(path + File.separator + files[0].getName())) {
                byte[] buf = new byte[8192];
                int len;
                while ((len = in.read(buf)) > 0) {
                    int CUR_PROGRESS = Math.round(100f * file.length() / files[0].length());
                    publishProgress(CUR_PROGRESS);
                    out.write(buf, 0, len);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        if (Build.VERSION.SDK_INT >= 24){
            Notification.Builder build = Notification.Builder.recoverBuilder(mContext, notification);
            build.setProgress(100, progress[0],false);
        }else{
            notification.contentView.setProgressBar(android.R.id.progress,100,progress[0],false);
        }
        notificationManager.notify(1, notification);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM