andy
andy

Reputation: 391

android app autoupdate if apk present then delete

I have tried (without success) to get my large apk updates to install (via adb) from a custom java app, despite the help from stackoverflow and several experiments that route always seems to fail (see Java Application to install APK on android).

The app and devices it is installed on are offline only and not published to the market.

I have decided to try a different route to the same problem; I can push the apk from the java app to /sdcard/MyApp/updates/update.apk

I would like when the user runs myapp it to check the presence of update.apk and if it is present run the update to myapp. When the update is complete I would like update.apk to be deleted (to prevent an update loop each time the app starts). I am new to android and am not really sure how to implement the above behaviour.

My code is pretty sparse "chunks" of functionality but included below to give an idea what I am thinking:

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
}

My questions are:

Is there a better way to implement the above required functionality? How can I be sure the update.apk has installed and worked before deleting it?

Thanks for the help, as I mentioned I am new to Java and android and trying to battle through.

EDIT: 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(" "); // 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);
    }

Andy

Upvotes: 1

Views: 1305

Answers (1)

David Wasser
David Wasser

Reputation: 95568

The approach you are taking is fine, and works well. You need to understand that your existing app will be shutdown (killed) in order to perform the update. The user will need to return to your app manually.

In order to delete the APK to avoid the infinite loop, you'll need to know the version number of the update. If you know that (maybe you have it as part of the filename, or some other way), you can compare it against the version of your app that is running. If they are the same, you can be sure that your update has been installed and you can delete the update APK.

To determine the version that is running, you can use the following:

    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
    }

Upvotes: 1

Related Questions