Android 如何仅以10%的增量调用NotificationManager
我有一个上传照片的服务。我有一个由NotificationManager更新的progressbar,但是它被称为multipletimesAndroid 如何仅以10%的增量调用NotificationManager,android,Android,我有一个上传照片的服务。我有一个由NotificationManager更新的progressbar,但是它被称为multipletimes IntentService[n相同的127行 我如何使经理只在上传10%的增量时通知 @Override public void writeTo(@NonNull BufferedSink sink) throws IOException { long fileLength = mFile.length();
IntentService[n相同的127行
我如何使经理只在上传10%的增量时通知
@Override
public void writeTo(@NonNull BufferedSink sink) throws IOException {
long fileLength = mFile.length();
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
FileInputStream in = new FileInputStream(mFile);
long uploaded = 0;
int read;
while (is_uploading && (read = in.read(buffer)) != -1) {
int percent = 10* (int) (10 * uploaded / fileLength);
mManager.notify(NOTIFICATION_ID, uploadingProgressNotification(String.valueOf(current_image_uploading+1), percent + 10));
uploaded += read;
sink.write(buffer, 0, read);
}
}
编辑:
Log.d(标记,“writeTo:test”);
仍被多次调用,尽管它应该只有10次
IntentService[n相同的346行
使用以下代码:
int percent = (int) (100 * uploaded / fileLength);
if(percent % 10 == 0 && lastPercent != percent){
lastPercent = percent;
mManager.notify(NOTIFICATION_ID, uploadingProgressNotification(String.valueOf(current_image_uploading+1), percent));
}
同时创建globle变量
public int lastPercent=0;
hmmm我试过了,但它仍被多次调用IntentService[n相同的1326行
int percent = (int) (100 * uploaded / fileLength);
if(percent % 10 == 0 && lastPercent != percent){
lastPercent = percent;
mManager.notify(NOTIFICATION_ID, uploadingProgressNotification(String.valueOf(current_image_uploading+1), percent));
}