我需要使用通知显示文件复制过程的进度。我解决了这个问题,但我的手机在这个过程中一直在肆虐。那我怎么能摆脱滞后?
我试图使用AsyncTask <...>来解决这个问题。如果我尝试在通知面板中向左或向右移动通知,则移动速度非常慢。随着复制过程的完成,通知开始表现为通常的通知而没有滞后。
我开始使用副本
new notification().execute(myFile, null, null);
复制课:
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);
}
}