Skip to content

Instantly share code, notes, and snippets.

@jimmy-collazos
Created April 27, 2012 14:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jimmy-collazos/2509913 to your computer and use it in GitHub Desktop.
Save jimmy-collazos/2509913 to your computer and use it in GitHub Desktop.
Class for check version instaled in Android App
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
public class App {
public static final String PREFERENCE_APP_KEY = "com.my.app.APP_PREFERENCE_PRIVATE";
public static final String PREFERENCE_VERSION_KEY = "com.my.app.APP_VERSION";
public static boolean setVersion(Context ctx){
long v = getVersion(ctx);
boolean out = false;
if(v>0){
SharedPreferences prf = getPreferences(ctx);
out = prf.edit().putLong(PREFERENCE_APP_KEY, v).commit();
}
return out;
};
public static long getVersion(Context ctx){
long v = 0L;
PackageInfo pInfo;
try {
pInfo = getPackageInfo(ctx);
v = pInfo.versionCode;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return v;
}
public static long getVersionInstalled(Context ctx){
SharedPreferences prf = getPreferences(ctx);
return prf.getLong(PREFERENCE_VERSION_KEY, 0L);
}
public static SharedPreferences getPreferences(Context ctx){
return ctx.getApplicationContext().getSharedPreferences(PREFERENCE_APP_KEY,
Context.MODE_PRIVATE);
}
public static PackageInfo getPackageInfo(Context ctx) throws NameNotFoundException{
return ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);
}
}
@jimmy-collazos
Copy link
Author

Example Use:

long versionInstalled = App.getVersionInstalled(this);
long current_v = App.getVersion(this);
if( versionInstalled  != current_v ){
    Log.w("MOV", "No veresion valid");  
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment