Android 判断App是否是第一次启动或安装

最近开发中遇到的一个需求,通过判断App是否是第一次启动或安装使用不同的启动动画。

这里我使用了SharedPreferences来实现该功能,思路很简单,通过SharedPreferences存储到手机中一些标志数据,启动时判断该数据即可。

下边是阅读以下博客后整理的工具接口
http://www.cnblogs.com/520-1314/p/5067454.html
https://www.jianshu.com/p/4a61f498300b

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

public interface MySharedPreferences {

    class SharedPreferencesUtil {
        private static final String FILE_NAME = "Config";
        private static SharedPreferences mPreferences;
        private static SharedPreferences.Editor mEditor;
        private static SharedPreferencesUtil mSharedPreferencesUtil;

        @SuppressLint("CommitPrefEdits")
        SharedPreferencesUtil(Context context) {
            mPreferences = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
            mEditor = mPreferences.edit();
        }
        
        public static SharedPreferencesUtil getInstance(Context context) {
            if (mSharedPreferencesUtil == null) {
                mSharedPreferencesUtil = new SharedPreferencesUtil(context);
            }
            return mSharedPreferencesUtil;
        }

        /**
         * 保存数据到文件
         */
        public void saveData(String key, Object data) {
            String type = data.getClass().getSimpleName();

            if ("Integer".equals(type)) {
                mEditor.putInt(key, (Integer) data);
            } else if ("Boolean".equals(type)) {
                mEditor.putBoolean(key, (Boolean) data);
            } else if ("String".equals(type)) {
                mEditor.putString(key, (String) data);
            } else if ("Float".equals(type)) {
                mEditor.putFloat(key, (Float) data);
            } else if ("Long".equals(type)) {
                mEditor.putLong(key, (Long) data);
            }

            SharedPreferencesCompat.apply(mEditor);
        }

        /**
         * 从文件中读取数据
         */
        public Object getData(String key, Object defValue) {

            String type = defValue.getClass().getSimpleName();

            //defValue为为默认值,如果当前获取不到数据就返回它
            if ("Integer".equals(type)) {
                return mPreferences.getInt(key, (Integer) defValue);
            } else if ("Boolean".equals(type)) {
                return mPreferences.getBoolean(key, (Boolean) defValue);
            } else if ("String".equals(type)) {
                return mPreferences.getString(key, (String) defValue);
            } else if ("Float".equals(type)) {
                return mPreferences.getFloat(key, (Float) defValue);
            } else if ("Long".equals(type)) {
                return mPreferences.getLong(key, (Long) defValue);
            }

            return null;
        }

        /**
         * 清除所有数据
         */
        public void clear() {
            mEditor.clear();
            SharedPreferencesCompat.apply(mEditor);
        }

        /**
         * 移除某个key值已经对应的值
         */
        public void remove(String key) {
            mEditor.remove(key);
            SharedPreferencesCompat.apply(mEditor);
        }

        /**
         * 查询某个key是否已经存在
         */
        public boolean contains(String key) {
            return mPreferences.contains(key);
        }

        /**
         * 返回所有的键值对
         */
        public Map<String, ?> getAll() {
            return mPreferences.getAll();
        }
    }

    /**
     * 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类
     */
    class SharedPreferencesCompat {
        private static final Method sApplyMethod = findApplyMethod();

        /**
         * 反射查找apply的方法
         */
        @SuppressWarnings({"unchecked", "rawtypes"})
        private static Method findApplyMethod() {
            try {
                Class clz = SharedPreferences.Editor.class;
                return clz.getMethod("apply");
            } catch (NoSuchMethodException ignored) {
            }

            return null;
        }

        /**
         * 如果找到则使用apply执行,否则使用commit
         */
        public static void apply(SharedPreferences.Editor editor) {
            try {
                if (sApplyMethod != null) {
                    sApplyMethod.invoke(editor);
                    return;
                }
            } catch (IllegalArgumentException ignored) {
            } catch (IllegalAccessException ignored) {
            } catch (InvocationTargetException ignored) {
            }
            editor.commit();
        }
    }
}

使用

//标志数据的key
String IS_FIRST_START=“is_first_start”;
//创建对象
final MySharedPreferences.SharedPreferencesUtil sharedPreferencesUtil = MySharedPreferences.SharedPreferencesUtil.getInstance(this);
//获取存储的判断是否是第一次启动,默认为true
boolean isFirst = (boolean) sharedPreferencesUtil.getData(IS_FIRST_START, true);

if(isFirst){
	sharedPreferencesUtil.saveData(IS_FIRST_START, false);
	//动画效果
}else{
	//动画效果
}
  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值