嗨,我有一个应用程序,我正在尝试向其添加内置更新程序。该应用程序包含一个按钮,可从我的服务器下载apk文件。服务器上的文件名命名如下:
“ App-1.apk”,
“ App-2.apk”,
“ App-3.apk”
...
该按钮当前的设置如下:
download = (Button) findViewById(R.id.bDownload);
versionNum = 3;
download.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent downloadFromServer = new Intent();
downloadFromServer.setAction(Intent.ACTION_VIEW);
downloadFromServer.addCategory(Intent.CATEGORY_BROWSABLE);
downloadFromServer.setData(Uri.parse("http://server.com/Files/App-" + versionNum + ".apk"));
startActivity(downloadFromServer);
}
});
检查服务器上可用版本最高的应用程序版本并选择该版本进行下载的好方法是什么?
编辑:如何使用Java检查服务器目录中编号最大的应用程序?
Edit2:这就是我最终要做的事情。这不是最好的解决方案,但目前已经足够了:
try {
PackageInfo appInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
installedVersion = appInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
// Handle exception
}
//Latest version available on my server. Must update this value for each new release
latestVersion = 3.1;
//Convert string value of installed version to double so that it can be compared with value of latest version
installedVersionValue = Double.parseDouble(installedVersion);
download = (Button) findViewById(R.id.bDownload);
download.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (installedVersionValue<latestVersion) { //If latest version available on server is greater than installed version, download the latest
Intent downloadFromServer = new Intent();
downloadFromServer.setAction(Intent.ACTION_VIEW);
downloadFromServer.addCategory(Intent.CATEGORY_BROWSABLE);
downloadFromServer.setData(Uri.parse("http://server.com/Files//App-" + latestVersion + ".apk"));
startActivity(downloadFromServer);
}
else if (installedVersionValue==latestVersion) { //If user clicks the update button while they already have the latest, let them no what's up
Toast.makeText(getApplicationContext(), "You are already running the latest version (" + installedVersionValue +")",
Toast.LENGTH_LONG).show();
}
}
});
最佳答案
您可以在清单中添加代码版本或应用程序版本,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.package.name"
android:versionCode="2"
android:versionName="1.1">
您可以检查它:
PackageInfo pinfo = getPackageManager().getPackageInfo(getPackageName(), 0);
int versionNumber = pinfo.versionCode;
您可以在服务器的db表中将代码数量设置为名称,然后检查该字段以更新您的应用程序。
您可以尝试如下操作:
向服务器传递这样的请求:
youServer/apkUpdate?versionCode={versionCode}
在您的服务器中,您可以使用以下方法:
@RequestMapping(method = RequestMethod.GET)
public void getUpdatedApk(
@RequestParam(value = "versionCode", required = true) final Integer versionCode,
@RequestParam(value = "androidVersion", required = false) final Integer androidVersion,
final HttpServletRequest request,
final HttpServletResponse response) throws Exception{
apkUpdateService.getUpdate(new Long(versionCode), response);
}
api更新服务在哪里:
public void getUpdate(Long currentVersionCode, HttpServletResponse response) throws Exception {
//get latest number of apk from a db field
Long dbVersionCode = apkUpdateRepository.findLastVersion();
ServletOutputStream servletOutputStream = null;
if (currentVersionCode == dbVersionCode){
LOG.info("You have the last version");
return;
}
if (currentVersionCode < dbVersionCode){
FileInputStream inputStream = null;
String filename = String.format(pathToApk, dbVersionCode);
try{
inputStream = new FileInputStream(filename);
servletOutputStream = response.getOutputStream();
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
servletOutputStream.write(buffer, 0, bytesRead);
}
servletOutputStream.flush();
LOG.info("File streamed");
LOG.info("Download "+filename);
}finally{
if (inputStream!=null){
inputStream.close();
}
if (servletOutputStream != null){
servletOutputStream.close();
}
}
}
}