【发布时间】:2014-03-22 03:45:32
【问题描述】:
您好,我有一个应用程序正在尝试添加内置更新程序。该应用程序包含一个从我的服务器下载 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();
}
}
});
【问题讨论】:
-
您是在询问将在服务器上执行的代码吗?
-
不,我正在寻找应用程序中的 java 解决方案。我不打算对服务器进行任何修改或发生任何事情。
-
服务器能否发送它包含的应用程序可以扫描的文件列表?