嗨,我有一个应用程序,我试图添加一个内置的更新程序。该应用程序包含一个从我的服务器下载apk文件的按钮。服务器上的文件名命名如下:
“的App-1.apk”, “应用程序 - 2.apk” “应用程序,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();
}
}
});
答案 0 :(得分:1)
您可以在清单中添加代码版本或应用版本,如下所示:
<?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();
}
}
}
}
答案 1 :(得分:0)
你可以举例如:
String installedVersion = "";
try {
PackageInfo manager = activity.getPackageManager().getPackageInfo(activity.getPackageName(), 0);
installedVersion = manager.versionCode + "";
} catch (PackageManager.NameNotFoundException e) {
// Handle exception
}
"http://server.com/Files/update?installed=" + installedVersion
在服务器端,您可以执行以下操作:
int installedVersion = ...
int latestVersion = ...
if(installedVersion < latestVersion){
//return file
}else{
//Return message: you have the latest
}
答案 2 :(得分:0)
我认为最简单的方法是按升序检查文件是否存在,而不是下载最后一个文件。文件存在检查已在此处得到解答:Check if file exists on remote server using its URL