Android device age

后端 未结 3 1972
北恋
北恋 2021-01-25 10:28

Is it possible to query the age of an android device? I want to know, how long a user owns his device. The age of the battery might be a good indicator, but I cannot find an app

3条回答
  •  天命终不由人
    2021-01-25 10:49

    There is no solid way to find out the device age but yes somehow we can figure out the age.

    Android devices are packaged with some pre-installed apps(Sytem applications) and when user owns it, he installs other apps(User applications). We can find out date of earliest installed user app and can predict the device age.

    * If device has been reset after use, it would be difficult to find device age.

    // @return earliest installed app's date.

    private static String getOldestAppsAge(Context context) {
        long appsAge = 0;
        try {
            appsAge = System.currentTimeMillis();
            PackageManager pkgManager = context.getPackageManager();
            List lsApp = pkgManager.getInstalledApplications(0);
            for (ApplicationInfo localApplicationInfo : lsApp) {
                String pkgName = localApplicationInfo.packageName;
                if ((localApplicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                    PackageInfo info = pkgManager.getPackageInfo(pkgName, 0);
                    long firstInstallTime = info.firstInstallTime;
                    if (firstInstallTime < appsAge && firstInstallTime > sGINGER_RELEASE_DATE) {
                        appsAge = info.firstInstallTime;
                    }
                }
            }
        } catch (PackageManager.NameNotFoundException ex) {
            ex.printStackTrace();
        }
        return DateUtils.convertIntoDesiredDateFormat(appsAge);
    }
    
    • Some android devices return epoch time (1970), so we must use some check condition : if (firstInstallTime > sGINGER_RELEASE_DATE)

提交回复
热议问题