package com.joy.library.utils;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.joy.library.BaseApplication;

/**
 * Created by KEVIN.DAI on 15/7/10.
 */
public class AppUtil {

    /**
     * 获取应用第一次安装时间
     *
     * @return
     */
    public static long getInstallAppTime() {

        long firstInstallTime = 0;

        try {

            Context ctx = BaseApplication.getContext();
            PackageInfo packageInfo = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0);
            firstInstallTime = packageInfo.firstInstallTime;// 应用第一次安装的时间

        } catch (Exception e) {

            e.printStackTrace();
        }

        return firstInstallTime;
    }
    /**
     * 获取VersionCode，默认返回1
     * @return
     */
    public static int getVersionCode() {

        try {

            Context ctx = BaseApplication.getContext();
            PackageInfo packInfo = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0);
            return packInfo.versionCode;

        } catch (PackageManager.NameNotFoundException e) {

//            if(LogMgr.isDebug())
                e.printStackTrace();
        }

        return 1;
    }

    public static String getVersionName() {

        try {

            Context ctx = BaseApplication.getContext();
            PackageInfo packInfo = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0);
            return packInfo.versionName == null ? "" : packInfo.versionName;

        } catch (Exception e) {

//            if(LogMgr.isDebug())
                e.printStackTrace();
        }

        return "";
    }
}

@@ -99,50 +99,8 @@
 nfo;
-%0Aimport android.content.pm.PackageManager;
 %0A%0Aim
@@ -714,922 +714,5 @@
   %7D%0A
-    /**%0A     * %E8%8E%B7%E5%8F%96VersionCode%EF%BC%8C%E9%BB%98%E8%AE%A4%E8%BF%94%E5%9B%9E1%0A     * @return%0A     */%0A    public static int getVersionCode() %7B%0A%0A        try %7B%0A%0A            Context ctx = BaseApplication.getContext();%0A            PackageInfo packInfo = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0);%0A            return packInfo.versionCode;%0A%0A        %7D catch (PackageManager.NameNotFoundException e) %7B%0A%0A//            if(LogMgr.isDebug())%0A                e.printStackTrace();%0A        %7D%0A%0A        return 1;%0A    %7D%0A%0A    public static String getVersionName() %7B%0A%0A        try %7B%0A%0A            Context ctx = BaseApplication.getContext();%0A            PackageInfo packInfo = ctx.getPackageManager().getPackageInfo(ctx.getPackageName(), 0);%0A            return packInfo.versionName == null ? %22%22 : packInfo.versionName;%0A%0A        %7D catch (Exception e) %7B%0A%0A//            if(LogMgr.isDebug())%0A                e.printStackTrace();%0A        %7D%0A%0A        return %22%22;%0A    %7D%0A%7D%0A
+%7D

删除获取versionCode和versionName的代码，用BuildConfig代替