问题
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 appropriate API. Optimum would be something the timestamp of the first device boot. Any Ideas?
回答1:
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<ApplicationInfo> 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)
回答2:
There are a few difficulties you may need to consider here:
1) Most android devices have (easily) removable batteries. I, for one, have several batteries that I alternate between when I use my phone.
2) Even if timestamps had been collected on boot, they would likely be deleted if the phone's owner wipes the device to install different ROMs.
I'd expect jcollum's solution of attempting to determine the model of the phone, or look at some piece of its hardware and determine an approximate release date of the model might be your closest solution. I doubt you'll find much accuracy in determining the age of the actual device. If there is, however, please correct me - I'm curious about this now too!
回答3:
That's an interesting question. There's no API to tell when the device was first purchased - but you can look for proxies.
- Read the SMS inbox - what's the earliest date a message was sent or received. (Of course, they could have deleted their older messages).
- Similarly, read the call log and see when they first received or made a call.
- The uptime can also be queried - but I guess most people switch their device off, or otherwise reboot it.
- Looking in the gallery, you could query the EXIF data and see when the earliest photograph was taken.
- With access to the file system, you could look at when certain files were created.
None of those are perfect, but they can be used to put some bounds on when the user first got their device.
来源:https://stackoverflow.com/questions/9242307/android-device-age