i try some method,but not success,help me.
Asked
Active
Viewed 3.3k times
45
-
22Could you add a few hundred words explaining what you are referring to? – CommonsWare May 14 '10 at 01:35
-
3I guess he wants to know when an app was installed. – Macarse May 14 '10 at 02:41
-
who tell me,how to get List application installed ,it installed by user,nou system. – jezz May 14 '10 at 07:22
5 Answers
65
PackageManager pm = context.getPackageManager();
ApplicationInfo appInfo = pm.getApplicationInfo("app.package.name", 0);
String appFile = appInfo.sourceDir;
long installed = new File(appFile).lastModified(); //Epoch Time

ShashiKant
- 210
- 2
- 8

yanchenko
- 56,576
- 33
- 147
- 165
-
5
-
9How about the actual first install time and NOT the update time? – Christopher Perry Aug 28 '12 at 05:15
-
1
61
In API level 9 (Gingerbread) and above, there's the PackageInfo.firstInstallTime field, holding milliseconds since the epoch:
packageManager.getPackageInfo(packageName, 0).firstInstallTime;
I have the following code to use it if available, and fall back to the apk modification time:
// 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;
}

orip
- 73,323
- 21
- 116
- 148
-
1Why is it called `firstInstallTime` ? Why first? Upon removal, it won't be restored on the next installation of it, no? – android developer Apr 02 '19 at 11:25
18
PackageInfo.firstInstallTime gives 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.Date or java.text.DateFormat in order to format this time.
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.
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);

Paolo Rovelli
- 9,396
- 2
- 58
- 37
-
2Its unnecessary to request "PackageManager.GET_PERMISSIONS" from the packagemanager if you only want those 2 times (just pass "0") – Patrick Dec 18 '16 at 11:43
-
How come it's called `firstInstallTime` ? Why first? Upon removal, it won't be restored on the next installation of it, no? – android developer Apr 02 '19 at 11:25
0
First install time as Kotlin extension:
fun Context.getFirstInstallDate(): Date? =
try {
val time: Long = packageManager.getPackageInfo(packageName, 0).firstInstallTime
Date(time)
} catch (e: Exception) {
e.printStackTrace()
null
}

Boken
- 4,825
- 10
- 32
- 42