Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get previous version code of app when installing new version programatically in android?

Tags:

android

I want to get the previous version code of an application when new version of the application is being installed.

What i tryied:

I have implemented following class and it will get run when new version is being installed. But i can only get the new version code.

 public class VersionContraller extends BroadcastReceiver {  

    @Override public void onReceive(Context context, Intent intent) {  

       if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction()))  { 

           if(intent.getData().getSchemeSpecificPart().equals(context.getPackageName())) { 


            try {
                 PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
                 int newVersion=pInfo.versionCode;   
                //I want to do some logic hear with older and new version of the application
            } catch (NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

           }
       }
    }
}

How can i get the previous version of the application inside above class?

like image 664
Dinesh Anuruddha Avatar asked Feb 06 '26 13:02

Dinesh Anuruddha


1 Answers

AFAIK you can't do this very easely. You can use SharedPreferences in your code, like this:

String newVersion;
try {
    PackageInfo pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
    int intVersion = pInfo.versionCode;
    newVersion = "" + intVersion;
} catch (NameNotFoundException e) {
    // Put a version you want if exception is launched
    newVersion = "something you want";
    e.printStackTrace();
}
SharedPreferences sharedPrefs = this.getSharedPreferences("your.package.app", Context.MODE_PRIVATE);
String savedVersion = "your.package.app.savedVersion";
String versionOnSP = sharedPrefs.getString(savedVersion, "previous");
if (versionOnSP.equalsIgnoreCase("previous") && newVersion.equalsIgnoreCase("versionTarget")) {
    // Your version is old; do some logic here with older version of the application
    ...
} else {
    // Your version is new, do whatever you want
}
// After that, save your current version
sharedPrefs.edit().putString(savedVersion, newVersion).commit();

In this way, at the first launch after update you will be able to do some operation because you haven't save a version on SharedPreferences in previous release.

But it is not 100% safe, because user can delete data of your app, and after a re-launch of the app you will go directly on first if. Maybe my idea need an improvement.

EDIT (after bgse comments)

My code must be placed into the first if:

public class VersionContraller extends BroadcastReceiver {  
    @Override public void onReceive(Context context, Intent intent) {  
        if(Intent.ACTION_PACKAGE_REPLACED.equals(intent.getAction()))  {
            // My code here
        }
    }
}
like image 83
JJ86 Avatar answered Feb 09 '26 03:02

JJ86