【安卓】判断"全新安装初次打开、升级后初次打开、第二次打开",比如可用于判断是否应显示"引导页"、!

来源:互联网 发布:matlab数据转为vtk 编辑:程序博客网 时间:2024/06/07 09:19

思路:

1.基于SharedPreferences,每次打开时,根据上次打开时记录的版本即可区分此次打开的情形。



StoredData.java:

1.Application.onCreate中调用StoredData.getThis().markOpenApp();即可。其他位置就可以根据getLaunchMode判断打开类型了。

package com.example.test;import android.app.Application;import android.content.SharedPreferences;import android.content.pm.PackageInfo;import android.content.pm.PackageManager;import android.text.TextUtils;public class StoredData {public static final int LMODE_NEW_INSTALL = 1; // 启动-模式,首次安装-首次启动、覆盖安装-首次启动、已安装-二次启动public static final int LMODE_UPDATE = 2;public static final int LMODE_AGAIN = 3;private boolean isOpenMarked = false;private int launchMode = LMODE_AGAIN; // 启动-模式private static StoredData instance;private SharedPreferences share; // 一般信息public static StoredData getThis() {if (instance == null)instance = new StoredData();return instance;}// -------启动状态------------------------------------------------------------// 标记-打开app,用于产生-是否首次打开public void markOpenApp() {// 防止-重复调用if (isOpenMarked)return;isOpenMarked = true;String lastVersion = share.getString("lastVersion", "");String thisVersion = getAppVersion();// 首次启动if (TextUtils.isEmpty(lastVersion)) {launchMode = LMODE_NEW_INSTALL;share.edit().putString("lastVersion", thisVersion).commit();}// 更新else if (!thisVersion.equals(lastVersion)) {launchMode = LMODE_UPDATE;share.edit().putString("lastVersion", thisVersion).commit();}// 二次启动(版本未变)elselaunchMode = LMODE_AGAIN;}public int getLaunchMode() {return launchMode;}// 首次打开,新安装、覆盖(版本号不同)public boolean isFirstOpen() {return launchMode != LMODE_AGAIN;}// -------------------------// 软件-版本public static String getAppVersion() {String versionName = "";Application app = MyApplication.getThis();try {PackageManager pkgMng = app.getPackageManager();PackageInfo pkgInfo = pkgMng.getPackageInfo(app.getPackageName(), 0);versionName = pkgInfo.versionName;} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}return versionName;}}


71 0
原创粉丝点击