package com.ysh.wpc.appupdate.util; import android.content.Context; import android.content.SharedPreferences; import android.content.pm.PackageInfo; import android.content.pm.PackageManager.NameNotFoundException; import android.preference.PreferenceManager; public class PackageUtils2 { public static boolean isFirstStartup(Context context) { try { PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); int currentVersion = info.versionCode; SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); int lastVersion = prefs.getInt("version_code", 0); if (currentVersion > lastVersion) { // 濡傛灉褰撳墠鐗堟湰澶т簬涓婃鐗堟湰锛岃鐗堟湰灞炰簬绗竴娆″惎鍔? // 灏嗗綋鍓嶇増鏈啓鍏reference涓紝鍒欎笅娆″惎鍔ㄧ殑鏃跺?锛屾嵁姝ゅ垽鏂紝涓嶅啀涓洪娆″惎鍔? prefs.edit().putInt("version_code", currentVersion).commit(); return true; } else { return false; } } catch (NameNotFoundException e) { e.printStackTrace(); } return false; } /** * 鑾峰彇versionCode * * @param context * @return 鐗堟湰鍙? */ public static int getVersionCode(Context context) { try { PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); return pi.versionCode; } catch (NameNotFoundException e) { e.printStackTrace(); return 0; } } }