关闭

android开发 判断app是否是第一次安装

标签: android开发判断app第一次安装首次安装
2306人阅读 评论(0) 收藏 举报
分类:
    一般用SharedPreferences来保存app的一些基本配置,是否是第一次安装也可以是用SharedPreferences来保存,这是封装的SharedPreferencesUtil工具类:
package com.zyl.vincent.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.zyl.vincent.log.LogUtil;

public class SharedPreferencesUtil {
    private Context mContext;
    private Editor mEditor;
    private SharedPreferences mPreferences;
    private String mFileName = "";
    private int mMode = 0;
    private static final String TAG = SharedPreferencesUtil.class.getSimpleName();

    /*public SharedPreferencesUtil(Context context) {
        this.mContext = context;
        this.mPreferences = context.getSharedPreferences(MoKeyApplication.SHARE_EASY_TOUCH, Context.MODE_PRIVATE);
        this.mEditor = this.mPreferences.edit();
        mFileName = MoKeyApplication.SHARE_EASY_TOUCH;
        mMode = Context.MODE_PRIVATE;
        LogUtil.D(TAG," create SharedPreferencesUtil; name : " + mFileName + "; mode : " + mMode);
    }*/

    public SharedPreferencesUtil(Context context, String fileName){
        this.mContext = context;
        this.mPreferences = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
        this.mEditor = this.mPreferences.edit();
        mFileName = fileName;
        mMode = Context.MODE_PRIVATE;
        LogUtil.D(TAG," create SharedPreferencesUtil; name : " + mFileName + "; mode : " + mMode);
    }

    public SharedPreferencesUtil(Context context, String fileName, int mode){
        this.mContext = context;
        this.mPreferences = context.getSharedPreferences(fileName, mode);
        this.mEditor = this.mPreferences.edit();
        mFileName = fileName;
        mMode = mode;
        LogUtil.D(TAG," create SharedPreferencesUtil; name : " + mFileName + "; mode : " + mMode);
    }

    // 读写配置文件
    public boolean putString(String name, String value) {
        mEditor.putString(name, value);
        boolean result = mEditor.commit();

        LogUtil.D(TAG, " put key : "+name+", value : "+value+" to file : "+mFileName+" result: "+result);
        return result;
    }

    public boolean putLong(String name, Long value) {
        mEditor.putLong(name, value);
        boolean result = mEditor.commit();

        LogUtil.D(TAG, " put key : "+name+", value : "+value+" to file : "+mFileName+" result: "+result);
        return result;
    }

    public boolean putInt(String name, int value) {
        mEditor.putInt(name, value);
        boolean result = mEditor.commit();

        LogUtil.D(TAG, " put key : "+name+", value : "+value+" to file : "+mFileName+" result: "+result);
        return result;
    }

    public boolean putBoolean(String name, Boolean value) {
        mEditor.putBoolean(name, value);
        boolean result = mEditor.commit();

        LogUtil.D(TAG, " put key : "+name+", value : "+value+" to file : "+mFileName+" result: "+result);
        return result;
    }

    public boolean remove(String name) {
        mEditor.remove(name);
        boolean result = mEditor.commit();

        LogUtil.D(TAG, " remove key : "+name+" from file : "+mFileName+" result: "+result);
        return result;
    }

    public boolean clear(){
        mEditor.clear();
        boolean result = mEditor.commit();

        LogUtil.D(TAG, " clear file : "+mFileName+" result: "+result);
        return result;
    }

    public long getLong(String key) {
        return mPreferences.getLong(key, 0);
    }

    public int getInt(String key) {
        return mPreferences.getInt(key, 0);
    }

    public Boolean getBoolean(String key) {
        return mPreferences.getBoolean(key, false);
    }

    public String getString(String key) {
        return mPreferences.getString(key, "");
    }

    public long getLong(String key, long defValue) {
        return mPreferences.getLong(key, defValue);
    }

    public int getInt(String key, int defValue) {
        return mPreferences.getInt(key, defValue);
    }

    public Boolean getBoolean(String key, boolean defValue) {
        return mPreferences.getBoolean(key, defValue);
    }

    public String getString(String key, String defValue) {
        return mPreferences.getString(key, defValue);
    }

    public Editor getEditor(){
        return mEditor;
    }
}

创建了一个类(Contants)用于保存配置新息:

package com.zyl.vincent.app;

/**
 * Created by Administrator on 2016/6/28.
 * 放置各种app的配置信息
 */
public final class Contants {
    public static final boolean LOG_SWITCH=true;//日志开关
    public static final String CONFIG = "config";//存放用户信息
    public static final int TIME=2;//   欢迎页面倒计时的秒数
    public static final String IS_FIRST_START="is_first_start";//app是否首次安装
    public static class WebUrl {
    }
}

准备工作做好了现在就开始判断app是否是第一次安装了..
在project的欢迎页面(WelcomActivity)判断,

private boolean isFirstStart;

在onCreate初始化SharedPreferencesUtil对象

 SharedPreferencesUtil shared=new SharedPreferencesUtil(WelcomActivity.this,Contants.CONFIG);
isFirstStart=shared.getBoolean(Contants.IS_FIRST_START);

判断app是否首次安装..

if(!isFirstStart){
        goGuide();//首次安装去引导页
    }else {
        goMain();//去主页
    }

最后还要在goGuide()方法中改变Contant.Is_FIRST_START的值
SharedPreferencesUtil shared=new SharedPreferencesUtil(this,Contants.CONFIG);
shared.putBoolean(Contants.IS_FIRST_START,true);
效果图就不上了..写过好几次,还是总结一次…

0
1

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:102921次
    • 积分:1968
    • 等级:
    • 排名:第19986名
    • 原创:86篇
    • 转载:8篇
    • 译文:0篇
    • 评论:52条
    最新评论