gpt4 book ai didi

java - 从服务器下载最新的文件版本

转载 作者:行者123 更新时间:2023-11-30 03:05:14 24 4
gpt4 key购买 nike

您好,我有一个应用程序正在尝试向其添加内置更新程序。该应用程序包含一个从我的服务器下载 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();
}
}
});

最佳答案

您可以在 list 中添加代码版本或应用程序版本,如您在此处所见:

<?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;

您可以在服务器上的数据库表中将代码数设置为名称,然后检查该字段以更新您的应用。

你可以尝试这样的事情:

向服务器传递这样的请求: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();
}
}
}
}

关于java - 从服务器下载最新的文件版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21920691/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com