提交 6a496762 authored 作者: vipcxj's avatar vipcxj

更新下载回调过于频繁,所以改成百分比每提升1%才执行回调

上级 f2c67d2d
......@@ -3,6 +3,7 @@ package com.bolan.android.modules.update;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Process;
import android.util.Log;
import com.bolan.android.modules.Utils;
......@@ -74,17 +75,21 @@ public class Updater extends ReactContextBaseJavaModule {
is = connection.getInputStream();
fos = new FileOutputStream(apkFile);
int count = 0;
int lastProgress = -1;
byte buf[] = new byte[1024 * 64];
WritableMap event;
do {
int numRead = is.read(buf);
count += numRead;
int progress = (int) (((float) count / length) * 100);
event = Arguments.createMap();
event.putInt("progress", progress);
event.putInt("current", count);
event.putInt("all", length);
emit("Updater/downloading", event);
if (progress != lastProgress) {
lastProgress = progress;
event = Arguments.createMap();
event.putInt("progress", progress);
event.putInt("current", count);
event.putInt("all", length);
emit("Updater/downloading", event);
}
if (numRead <= 0) {
event = Arguments.createMap();
event.putInt("progress", progress);
......@@ -107,7 +112,7 @@ public class Updater extends ReactContextBaseJavaModule {
return;
}
getCurrentActivity().startActivity(i);
android.os.Process.killProcess(android.os.Process.myPid());
Process.killProcess(Process.myPid());
promise.resolve(null);
} catch (MalformedURLException e) {
promise.reject("ERROR_UPDATE", "invalid url: " + sUrl);
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论