Created
April 27, 2012 14:55
-
-
Save jimmy-collazos/2509913 to your computer and use it in GitHub Desktop.
Class for check version instaled in Android App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example Use: