android应用程序自动更新如果存在apk,则删除
尽管有stackoverflow的帮助和一些route似乎总是失败的实验(请参阅),我还是尝试(没有成功)从一个定制java应用程序(通过adb)安装我的大型apk更新 安装该应用程序的应用程序和设备仅在脱机状态下运行,不会发布到市场上 我决定尝试一种不同的方法来解决同一个问题;我可以将apk从java应用程序推送到/sdcard/MyApp/updates/update.apk 我想在用户运行myapp时检查update.apk是否存在,如果存在,请运行myapp更新。更新完成后,我希望update.apk被删除(以防止每次应用程序启动时出现更新循环)。我是android新手,不确定如何实现上述行为 我的代码是相当稀疏的“块”功能,但包含在下面,让我了解我的想法:android应用程序自动更新如果存在apk,则删除,android,android-intent,Android,Android Intent,尽管有stackoverflow的帮助和一些route似乎总是失败的实验(请参阅),我还是尝试(没有成功)从一个定制java应用程序(通过adb)安装我的大型apk更新 安装该应用程序的应用程序和设备仅在脱机状态下运行,不会发布到市场上 我决定尝试一种不同的方法来解决同一个问题;我可以将apk从java应用程序推送到/sdcard/MyApp/updates/update.apk 我想在用户运行myapp时检查update.apk是否存在,如果存在,请运行myapp更新。更新完成后,我希望upd
if (update.exists()) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/MyApp/updates" + "updates.apk")), "application/vnd.android.package-archive");
startActivity(intent);
}
//add a delete to update.apk here AFTER it has finished installing
}
我的问题是:
是否有更好的方法来实现上述所需的功能?
在删除update.apk之前,如何确保它已安装并正常工作
谢谢你的帮助,正如我提到的,我是Java和android新手,正在努力克服困难
编辑:我使用的最终解决方案:
if (updateTxt.exists()) {
try {
BufferedReader br = new BufferedReader(
new FileReader(updateTxt));
String line;
while ((line = br.readLine()) != null) {
String line2[] = line.split(" "); // split the string to get
// the
// progress
myUpdateVersion = Integer.parseInt(line2[0]); // [0] is the
// value we
// are
// interested
// in
// so set
// it.
}
} catch (IOException ex) {
return;
}
} else {
// no update so do nothing
}
if (updateApk.exists()) {
// updateIntent();
// now check the version of the update file to see if it can be
// deleted
PackageManager packageManager = getPackageManager();
PackageInfo apkPackageInfo = packageManager.getPackageInfo(
"com.myapp.myapp", 0);
if (apkPackageInfo != null) {
if (apkPackageInfo.versionCode == myUpdateVersion) {
// Update has been installed. Delete update APK
updateApk.delete();
} else {
// Update needs to be installed
updateIntent();
}
} else {
// no update so do nothing
}
}
} // end updateApk
public void updateIntent() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.fromFile(new File(Environment.getExternalStorageDirectory()
+ "/updates/update.apk")),
"application/vnd.android.package-archive");
startActivity(intent);
}
安迪你采用的方法很好,效果也很好。您需要了解,为了执行更新,您现有的应用程序将被关闭(终止)。用户需要手动返回到您的应用程序 为了删除APK以避免无限循环,您需要知道更新的版本号。如果您知道这一点(可能是文件名的一部分,或者其他方式),您可以将其与正在运行的应用程序版本进行比较。如果它们相同,则可以确保已安装更新,并且可以删除更新APK 要确定正在运行的版本,可以使用以下命令:
PackageManager packageManager = getPackageManager();
PackageInfo apkPackageInfo = packageManager.getPackageInfo("your.package.name", 0);
if (apkPackageInfo != null) {
if (apkPackageInfo.versionCode == myUpdateVersion) {
// Update has been installed. Delete update APK
} else {
// Update needs to be installed
}
David,谢谢你的回答,我已经编辑了上面的帖子,展示了我最终使用的代码。