I tried (without success) to get my big apk updates to install (via adb) from a custom java application, despite help from stackoverflow and a few experiments that always seem to fail (see Java app for installing APKs on android ).
The application and the devices on which it is installed are only offline and are not published on the market.
I decided to try a different route to the same problem; I can press apk from java application on /sdcard/MyApp/updates/update.apk
I would like when the user launches myapp to check for update.apk, and if it is present, run the update for myapp. When the update is complete, I would like to remove update.apk (to prevent the update cycle every time the application starts). I am new to android and not quite sure how to implement the behavior described above.
My code is quite rare "pieces" of functionality, but the ones below to give an idea of ββwhat I think:
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);
}
}
My questions:
Is there a better way to implement the above required features? How can I make sure update.apk installed and worked before uninstalling?
Thanks for the help, as I said, I'm new to Java and android and trying to fight.
EDIT: The final solution I am using:
if (updateTxt.exists()) {
try {
BufferedReader br = new BufferedReader(
new FileReader(updateTxt));
String line;
while ((line = br.readLine()) != null) {
String line2[] = line.split(" ");
myUpdateVersion = Integer.parseInt(line2[0]);
}
} catch (IOException ex) {
return;
}
} else {
}
if (updateApk.exists()) {
PackageManager packageManager = getPackageManager();
PackageInfo apkPackageInfo = packageManager.getPackageInfo(
"com.myapp.myapp", 0);
if (apkPackageInfo != null) {
if (apkPackageInfo.versionCode == myUpdateVersion) {
updateApk.delete();
} else {
updateIntent();
}
} else {
}
}
}
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);
}
Andy