Good luck

自我充实,写下错误,以后自己看

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

    一般用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);
效果图就不上了..写过好几次,还是总结一次…

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/pkandroid/article/details/52352728
个人分类: android 开发 综合
上一篇android开发 倒计时和计时的实现
下一篇Android webview支持type="file" 打开相机和相册,支持小米(亲测6.0通过)
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭