Android常用工具类总结(一)应用国家,安装天数,版本号,是否debug获取

应用国家安装天数获取

应用安装天数获取

记录应用第一次启动的时间,从而当前安装天数,需在应用启动时调用刷新存储的数据

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

/**
 * 用户数据记录
 * 记录首次安装时间
 * 记录升级首次启动时间
 * Created by zhuxiaoxin
 */

public class UserDataRecord {

    //存储用户数据的sp
    private static final String SP_USER_DATA_RECORD = "sp_user_data_record";

    //应用首次安装时间,sp字段名
    private static final String FIRST_INSTALL_TIME = "first_install_time";

    //应用首次启动时间,包括首次安装启动时间和升级用户启动时间,sp字段名
    private static final String INSTALL_AND_UPGRADE_ENTER_TIME = "install_and_upgrade_enter_time";

    //应用上次安装版本,sp字段名
    private static final String LAST_INSTALL_VERSION = "last_install_version";

    //用户首次安装的版本,sp字段名
    private static final String FIRST_INSTALL_VERSION = "first_install_version";

    //应用上次启动的版本号
    private static int sLastInstallVersion = 0;

    //应用首次安装时间
    private static long sFirstInstallTime = 0;

    //应用首次使用时间(包括首次安装用户第一次启动和升级用户的第一次启动)
    private static long sFirstEnterTime = 0;

    //首次安装应用的版本号
    private static int sFirstInstallVersion = 0;

    //是否已经刷新数据
    private static boolean sRefresh = false;


    public static void refreshData(Context context){

        SharedPreferences sp = context.getSharedPreferences(SP_USER_DATA_RECORD, Context.MODE_MULTI_PROCESS);

        sLastInstallVersion = sp.getInt(LAST_INSTALL_VERSION, 0);
        sFirstInstallTime = sp.getLong(FIRST_INSTALL_TIME, System.currentTimeMillis());
        sFirstEnterTime = sp.getLong(INSTALL_AND_UPGRADE_ENTER_TIME, System.currentTimeMillis());
        sFirstInstallVersion = sp.getInt(FIRST_INSTALL_VERSION, 0);

        int curVersionCode = DeviceUtil.getAppVersionCode(context);//当前应用版本号

        if(curVersionCode > 0){
            SharedPreferences.Editor editor = sp.edit();
            if (sFirstInstallVersion == 0){
                //首次安装第一次启动,存储上一次版本,首次安装时间,首次启动时间,首次安装的版本号
                editor.putInt(LAST_INSTALL_VERSION, curVersionCode)
                        .putLong(FIRST_INSTALL_TIME, System.currentTimeMillis())
                        .putLong(INSTALL_AND_UPGRADE_ENTER_TIME, System.currentTimeMillis())
                        .putInt(FIRST_INSTALL_VERSION, curVersionCode).apply();
                sFirstInstallVersion = curVersionCode;
                sFirstEnterTime = System.currentTimeMillis();
                sFirstInstallTime = System.currentTimeMillis();
            } else if (sLastInstallVersion != curVersionCode){
                //升级或者降级安装第一次启动,存储上一次版本,首次启动时间
                editor.putInt(LAST_INSTALL_VERSION, curVersionCode)
                        .putLong(INSTALL_AND_UPGRADE_ENTER_TIME, System.currentTimeMillis()).apply();
                sFirstEnterTime = System.currentTimeMillis();
            }
        }

        sRefresh = true;
    }

    /**
     * 是否时新用户首次启动
     * @param context
     * @return
     */
    public static boolean isNewUserFirstRun(Context context){
        if (!sRefresh){
            refreshData(context);
        }
        return sLastInstallVersion == 0;
    }

    /**
     * 是否时升级用户首次启动
     * @return
     */
    public static boolean isUpgradeUserFirstRun(Context context){
        if (!sRefresh){
            refreshData(context);
        }
        return sLastInstallVersion < DeviceUtil.getAppVersionCode(context);
    }

    /**
     * 是否是升级用户
     */
    public static boolean isUpgradeUser(Context context){
        if (!sRefresh){
            refreshData(context);
        }
        return sFirstInstallVersion < DeviceUtil.getAppVersionCode(context);
    }

    /**
     * 获得应用首次启动时间
     */
    public static long getFirstInstallOrUpgradeTime(Context context){
        if (!sRefresh){
            refreshData(context);
        }
        return sFirstEnterTime;
    }

    /**
     * 获得应用首次安装时间
     */
    public static long getFirstInstallTime(Context context){
        if (!sRefresh){
            refreshData(context);
        }
        return sFirstInstallTime;
    }

}

获取国家代号

获取国家代号的方法有:通过sim卡获取,通过安卓的Locale类获取,通过ip获取。

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import java.util.Locale;

/**
 * 常用工具类
 * Created by zhuxiaoxin on 2018/6/21.
 */

public class DeviceUtil {

    /**
     * 根据sim卡获得国家代号
     * @param context
     * @return
     */
    public static String getCountryBySim(Context context) {
        String result = null;
        if (context != null) {
            TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if (manager.getSimState() == TelephonyManager.SIM_STATE_READY) {
                String simOperator = manager.getSimOperator();
                String simCountry = manager.getSimCountryIso();
                if (!android.text.TextUtils.isEmpty(simOperator)) {
                    Log.d("simOperator", simOperator);
                    if (simOperator.startsWith("404") || simOperator.startsWith("405")) {
                        result = "in";
                    } else if (simOperator.startsWith("310")) {
                        result = "us";
                    } else if (simOperator.startsWith("460")) {
                        result = "cn";
                    } else if (simOperator.startsWith("515")) {
                        result = "ph";
                    } else if (simOperator.startsWith("510")) {
                        result = "id";
                    } else if (simOperator.startsWith("724")) {
                        result = "br";
                    } else if (simOperator.startsWith("250")) {
                        result = "ru";
                    } else if (simOperator.startsWith("334")) {
                        result = "mx";
                    } else if (simOperator.startsWith("286")) {
                        result = "tr";
                    } else if (simOperator.startsWith("432")) {
                        result = "ir";
                    } else if (simOperator.startsWith("502")) {
                        result = "my";
                    } else if (simOperator.startsWith("410")) {
                        result = "pk";
                    } else if (simOperator.startsWith("602")) {
                        result = "eg";
                    } else if (simOperator.startsWith("470")) {
                        result = "bd";
                    } else if (simOperator.startsWith("722")) {
                        result = "ar";
                    } else if (simOperator.startsWith("214")) {
                        result = "es";
                    } else if (simOperator.startsWith("604")) {
                        result = "ma";
                    } else if (simOperator.startsWith("520")) {
                        result = "th";
                    } else if (simOperator.startsWith("621")) {
                        result = "ng";
                    } else if (simOperator.startsWith("234")) {
                        result = "gb";
                    } else if (simOperator.startsWith("226")) {
                        result = "ro";
                    }
                }
                if (android.text.TextUtils.isEmpty(result) && !android.text.TextUtils.isEmpty(simCountry)) {
                    result = simCountry;
                }
            }
        }
        if (android.text.TextUtils.isEmpty(result)) {
            return "unknow";
        }
        return result;
    }

    /**
     * 获得本机国家代号
     * @param context
     * @return
     */
    public static String getLocalCountry(Context context) {
        String country = Locale.getDefault().getCountry();
        // 标准返回是2位
        if (country.length() != 2) {
            country = context.getResources().getConfiguration().locale.getCountry().toUpperCase();
        }
        if (country.length() > 2) {
            country = country.substring(0, 2);
        }
        return country;
    }

    /**
     * 获得当前是否处于debug模式
     * @param context
     * @return
     */
    public boolean isApkDebuggable(Context context){
        try{
            ApplicationInfo applicationInfo = context.getApplicationInfo();
            return (applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
        } catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 获得本机语言代号
     * @param context
     * @return
     */
    public static String getLocalLanguage(Context context) {
        String language = Locale.getDefault().getLanguage().toLowerCase();
        // 标准返回是2位
        if (language.length() != 2) {
            language = context.getResources().getConfiguration().locale.getLanguage().toLowerCase();
        }
        if (language.length() > 2) {
            language = language.substring(0, 2);
        }
        return language;
    }

    /**
     * 获得当前应用版本号
     */
    public static int getAppVersionCode(Context context){
        PackageManager packageManager = context.getPackageManager();
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 获得当前应用版本名称
     */
    public static String getAppVersionName(Context context){
        PackageManager packageManager = context.getPackageManager();
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            return packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 获取安装天数,从1计数
     */
    public static int getInstallDays(Context context){
        long nowTime = System.currentTimeMillis();
        long firstEnterTime = UserDataRecord.getFirstInstallOrUpgradeTime(context);
        long day = 1000 * 60 * 60 * 24;
        return (int)((nowTime - firstEnterTime) / day) + 1;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值