应用程序更新在启动时发生在应用程序中。该应用程序在服务器上检查最新版本,如果有更新版本可用,则下载并存储最新的 .apk 文件。然后应用程序启动更新过程。
有时发生的情况是,在下载并尝试安装更新后会出现一条消息。“解析错误。解析包时出现问题。” 我们在该领域有 60 多台 Android 设备,除了大约 10 台之外,大多数更新都适用于这些设备。
然后在一个使用相同更新过程的新项目中,我们的平板电脑拒绝使用相同的错误消息进行更新。这种情况在同一台平板电脑上反复发生,尽管在另一台平板电脑(不同品牌和操作系统)上运行良好。
设备品牌或操作系统版本之间似乎没有关联。在平板电脑上,很明显 .apk 文件没有完全下载。但是,从 Internet 浏览器进行手动下载和更新是可行的。
因此,尝试在某些设备上总结 .apk 文件下载未完成且安装失败。这可能是因为网络带宽限制吗?但是,该领域的这 10 台平板电脑肯定会在某个时候更新,因为每次应用程序启动时都会启动更新过程,我可以看到这些应用程序一次又一次地使用但没有更新。
很抱歉漫无边际,但我想了解很多信息。下面的这些代码行会在 5 秒后停止下载吗?两次超时有什么区别?
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
以下是标准更新AsyncTask
。任何帮助,将不胜感激。我现在有点困惑,时间不在我这边。
private class UpdateDownloadTask extends AsyncTask<Void, Void, String> {
HttpClient httpclient;
HttpGet httpget;
HttpResponse response;
HttpEntity httpentity;
OutputStream outputStream;
protected void onPreExecute () {
//do not lock screen or drop connection to server on login
activity.getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
//initiate progress dialogue to block user input during initial data retrieval
ProcessingDialog = ProgressDialog.show(context, "Please Wait", "Downloading Updates", true,false);
}
@Override
protected String doInBackground(Void... nothing) {
try {
//set timeouts for httpclient
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
//setup http get
httpclient = new DefaultHttpClient(httpParameters);
httpget = new HttpGet(uriApk);
// Execute HTTP Get Request
response = httpclient.execute(httpget);
httpentity = response.getEntity();
//create location to store apk file
String path = Environment.getExternalStorageDirectory() + "/download/";
File file = new File(path);
file.mkdirs(); //if download folder not already exist
File outputFile = new File(file, apkName);
//write downloaded file to location
outputStream = new FileOutputStream(outputFile, false);
httpentity.writeTo(outputStream);
outputStream.flush();
outputStream.close();
return "success";
}
catch (Exception e) {
return "error: " + e.toString();
}
}
@Override
protected void onPostExecute(String result) {
//check if result null or empty
if (result.length() == 0 || result == null) {
Toast.makeText(context, "Could Not Download Updates, Please Try Again. Closing Application.", Toast.LENGTH_LONG).show();
activity.finish();
}
//update downloaded
if (result.equals("success")) {
//install downloaded .apk file
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + apkName)), "application/vnd.android.package-archive");
activity.startActivity(intent);
//activity.finish();
}
//update not downloaded
else {
Toast.makeText(context, "Could Not Download Updates, Please Try Again. Closing Application.", Toast.LENGTH_LONG).show();
activity.finish();
}
//close update dialog
try {
ProcessingDialog.dismiss();
} catch (Exception e) {
// nothing
}
//release screen lock
activity.getWindow().clearFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}