如何从android获取应用程序安装时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2831333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get app install time from android
提问by jezz
i try some method,but not success,help me.
我尝试了一些方法,但没有成功,帮帮我。
回答by yanchenko
PackageManager pm = context.getPackageManager();
ApplicationInfo appInfo = pm.getApplicationInfo("app.package.name", 0);
String appFile = appInfo.sourceDir;
long installed = new File(appFile).lastModified(); //Epoch Time
回答by orip
In API level 9 (Gingerbread) and above, there's the PackageInfo.firstInstallTimefield, holding milliseconds since the epoch:
在 API 级别 9 (Gingerbread) 及更高版本中,有PackageInfo.firstInstallTime字段,保存自纪元以来的毫秒数:
packageManager.getPackageInfo(packageName, 0).firstInstallTime;
I have the following code to use it if available, and fall back to the apk modification time:
如果可用,我有以下代码可以使用它,并回退到apk修改时间:
// return install time from package manager, or apk file modification time,
// or null if not found
public Date getInstallTime(
PackageManager packageManager, String packageName) {
return firstNonNull(
installTimeFromPackageManager(packageManager, packageName),
apkUpdateTime(packageManager, packageName));
}
private Date apkUpdateTime(
PackageManager packageManager, String packageName) {
try {
ApplicationInfo info = packageManager.getApplicationInfo(packageName, 0);
File apkFile = new File(info.sourceDir);
return apkFile.exists() ? new Date(apkFile.lastModified()) : null;
} catch (NameNotFoundException e) {
return null; // package not found
}
}
private Date installTimeFromPackageManager(
PackageManager packageManager, String packageName) {
// API level 9 and above have the "firstInstallTime" field.
// Check for it with reflection and return if present.
try {
PackageInfo info = packageManager.getPackageInfo(packageName, 0);
Field field = PackageInfo.class.getField("firstInstallTime");
long timestamp = field.getLong(info);
return new Date(timestamp);
} catch (NameNotFoundException e) {
return null; // package not found
} catch (IllegalAccessException e) {
} catch (NoSuchFieldException e) {
} catch (IllegalArgumentException e) {
} catch (SecurityException e) {
}
// field wasn't found
return null;
}
private Date firstNonNull(Date... dates) {
for (Date date : dates)
if (date != null)
return date;
return null;
}
回答by Paolo Rovelli
PackageInfo.firstInstallTimegives you the install time in "Unix time"(the time in milliseconds since "the epoch", i.e. January 1, 1970 00:00:00 UTC). You may use java.util.Dateor java.text.DateFormatin order to format this time.
PackageInfo.firstInstallTime以“Unix 时间”为您提供安装时间(自“纪元”以来的毫秒数,即1970 年 1 月 1 日 00:00:00 UTC)。您可以使用java.util.Date或java.text.DateFormat来格式化这次。
private static final String TAG = "MyActivity";
...
packageName = ...
...
try {
PackageInfo packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS);
Date installTime = new Date( packageInfo.firstInstallTime );
Log.d(TAG, "Installed: " + installTime.toString());
Date updateTime = new Date( packageInfo.lastUpdateTime );
Log.d(TAG, "Updated: " + updateTime.toString());
}
catch ( PackageManager.NameNotFoundException e ) {
e.printStackTrace();
}
You can also change the date format with java.text.SimpleDateFormat.
您还可以使用java.text.SimpleDateFormat更改日期格式。
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String installTime = dateFormat.format( new Date( packageInfo.firstInstallTime ) );
Log.d(TAG, "Installed: " + installTime);
String updateTime = dateFormat.format( new Date( packageInfo.lastUpdateTime ) );
Log.d(TAG, "Updated: " + updateTime);