Android 扩展文件下载示例未显示全部进度
目前,我正在准备将扩展文件包含到已开发项目中的演示。 我已经注意到了这一点 经过很多努力,我能够运行演示应用程序,但在显示下载数据的进度时出现问题 我在play store上上传了obb文件,该文件的大小为1060024字节,磁盘大小为1060864字节 下面是我认为可能存在问题的代码片段Android 扩展文件下载示例未显示全部进度,android,progress-bar,apk-expansion-files,Android,Progress Bar,Apk Expansion Files,目前,我正在准备将扩展文件包含到已开发项目中的演示。 我已经注意到了这一点 经过很多努力,我能够运行演示应用程序,但在显示下载数据的进度时出现问题 我在play store上上传了obb文件,该文件的大小为1060024字节,磁盘大小为1060864字节 下面是我认为可能存在问题的代码片段 private static final XAPKFile[] xAPKS = { new XAPKFile(true,6,1060024L) }; void validateXAPKZipFiles()
private static final XAPKFile[] xAPKS = { new XAPKFile(true,6,1060024L) };
void validateXAPKZipFiles() {
AsyncTask<Object, DownloadProgressInfo, Boolean> validationTask = new AsyncTask<Object, DownloadProgressInfo, Boolean>() {
@Override
protected void onPreExecute() {
mDashboard.setVisibility(View.VISIBLE);
mCellMessage.setVisibility(View.GONE);
mStatusText.setText(R.string.text_verifying_download);
mPauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mCancelValidation = true;
}
});
mPauseButton.setText(R.string.text_button_cancel_verify);
super.onPreExecute();
}
@Override
protected Boolean doInBackground(Object... params) {
for (XAPKFile xf : xAPKS) {
String fileName = Helpers.getExpansionAPKFileName(
MainActivity.this, xf.mIsMain, xf.mFileVersion);
if (!Helpers.doesFileExist(MainActivity.this, fileName,
xf.mFileSize, true)) {
return false;
}
fileName = Helpers.generateSaveFileName(MainActivity.this,
fileName);
ZipResourceFile zrf;
byte[] buf = new byte[1024 * 256];
try {
zrf = new ZipResourceFile(fileName);
ZipEntryRO[] entries = zrf.getAllEntries();
/**
* First calculate the total compressed length
*/
long totalCompressedLength = 0;
for (ZipEntryRO entry : entries) {
totalCompressedLength += entry.mCompressedLength;
}
float averageVerifySpeed = 0;
long totalBytesRemaining = totalCompressedLength;
long timeRemaining;
/**
* Then calculate a CRC for every file in the Zip file,
* comparing it to what is stored in the Zip directory.
* Note that for compressed Zip files we must extract
* the contents to do this comparison.
*/
for (ZipEntryRO entry : entries) {
if (-1 != entry.mCRC32) {
long length = entry.mUncompressedLength;
CRC32 crc = new CRC32();
DataInputStream dis = null;
try {
dis = new DataInputStream(
zrf.getInputStream(entry.mFileName));
long startTime = SystemClock.uptimeMillis();
while (length > 0) {
int seek = (int) (length > buf.length ? buf.length
: length);
dis.readFully(buf, 0, seek);
crc.update(buf, 0, seek);
length -= seek;
long currentTime = SystemClock
.uptimeMillis();
long timePassed = currentTime
- startTime;
if (timePassed > 0) {
float currentSpeedSample = (float) seek
/ (float) timePassed;
if (0 != averageVerifySpeed) {
averageVerifySpeed = SMOOTHING_FACTOR
* currentSpeedSample
+ (1 - SMOOTHING_FACTOR)
* averageVerifySpeed;
} else {
averageVerifySpeed = currentSpeedSample;
}
totalBytesRemaining -= seek;
timeRemaining = (long) (totalBytesRemaining / averageVerifySpeed);
Log.e("Remaining Size is :: ",
" "
+ (totalCompressedLength - totalBytesRemaining));
this.publishProgress(new DownloadProgressInfo(
totalCompressedLength,
totalCompressedLength
- totalBytesRemaining,
timeRemaining,
averageVerifySpeed));
}
startTime = currentTime;
if (mCancelValidation)
return true;
}
if (crc.getValue() != entry.mCRC32) {
Log.e(Constants.TAG,
"CRC does not match for entry: "
+ entry.mFileName);
Log.e(Constants.TAG, "In file: "
+ entry.getZipFileName());
return false;
}
} finally {
if (null != dis) {
dis.close();
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
@Override
protected void onProgressUpdate(DownloadProgressInfo... values) {
onDownloadProgress(values[0]);
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Boolean result) {
if (result) {
mDashboard.setVisibility(View.VISIBLE);
mCellMessage.setVisibility(View.GONE);
mStatusText.setText(R.string.text_validation_complete);
mPauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
mPauseButton.setText(android.R.string.ok);
} else {
mDashboard.setVisibility(View.VISIBLE);
mCellMessage.setVisibility(View.GONE);
mStatusText.setText(R.string.text_validation_failed);
mPauseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
mPauseButton.setText(android.R.string.cancel);
}
super.onPostExecute(result);
}
};
validationTask.execute(new Object());
}
private static final XAPKFile[]xAPKS={new XAPKFile(true,61060024l)};
void validateXAPKZipFiles(){
AsyncTask validationTask=新建AsyncTask(){
@凌驾
受保护的void onPreExecute(){
mDashboard.setVisibility(View.VISIBLE);
mCellMessage.setVisibility(View.GONE);
mStatusText.setText(R.string.text\u验证\u下载);
mPauseButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
McCancelValidation=真;
}
});
mPauseButton.setText(R.string.text\u按钮\u取消\u验证);
super.onPreExecute();
}
@凌驾
受保护的布尔doInBackground(对象…参数){
for(XAPKFile xf:xAPKS){
字符串文件名=Helpers.getExpansionAPKFileName(
MainActivity.this、xf.mismin、xf.mFileVersion);
如果(!Helpers.doesfile)存在(MainActivity.this,文件名,
xf.mFileSize,真){
返回false;
}
fileName=Helpers.generateSaveFileName(MainActivity.this,
文件名);
zrf;
字节[]buf=新字节[1024*256];
试一试{
zrf=新ZipResourceFile(文件名);
ZipEntryRO[]条目=zrf.getAllEntries();
/**
*首先计算总压缩长度
*/
长总压缩长度=0;
用于(ZipEntryRO条目:条目){
总压缩长度+=entry.mccompressedlength;
}
浮动平均验证速度=0;
long TotalByTes剩余=总压缩长度;
剩余时间长;
/**
*然后计算Zip文件中每个文件的CRC,
*将其与Zip目录中存储的内容进行比较。
*请注意,对于压缩的Zip文件,我们必须提取
*进行此比较的内容。
*/
用于(ZipEntryRO条目:条目){
如果(-1!=entry.mCRC32){
long length=entry.muncressedlength;
CRC32 crc=新的CRC32();
DataInputStream dis=null;
试一试{
dis=新数据输入流(
getInputStream(entry.mFileName));
long startTime=SystemClock.uptimeMillis();
而(长度>0){
int seek=(int)(长度>buf.length?buf.length
:长度);
dis.readFully(buf,0,seek);
crc.update(buf,0,seek);
长度-=寻道;
长currentTime=系统时钟
.uptimeMillis();
长时间通过=当前时间
-开始时间;
如果(时间经过>0){
浮点currentSpeedSample=(浮点)寻道
/(浮动)时间流逝;
如果(0!=平均验证速度){
averageVerifySpeed=平滑系数
*当前速度样本
+(1-平滑系数)
*平均验证速度;
}否则{
averageVerifySpeed=currentSpeedSample;
}
TotalBytes剩余-=搜索;
剩余时间=(长)(总字节剩余/平均验证速度);
Log.e(“剩余大小为::”,
" "
+(总压缩长度-总剩余字节数));
此.publishProgress(新下载ProgressInfo(
总压缩长度,
总压缩长度
-剩余的总字节数,
剩余时间,
平均验证速度);
}