Android app autoupdate if apk is present then uninstall

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

}
 //add a delete to update.apk here AFTER it has finished installing
}

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

+1
1

, , . , (), . .

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
    }
+1

Source: https://habr.com/ru/post/1539255/


All Articles