android app是否安装程序,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); 效果图就不上了..写过好几次,还是总结一次…

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值