Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get app install time from android

Tags:

android

i try some method,but not success,help me.

like image 299
jezz Avatar asked May 14 '10 01:05

jezz


People also ask

How can I tell how long an app was installed?

Navigate to APPS and select the app u want by long press. And then hit the Info button on top right corner. Thats it , it will show u the modified or installed time.

Why does it take so long to install an app on my phone?

The problem persists even if you disable Wi-Fi–the most common reason why is a combination of two issues: DNS and Google Play cache. Sometimes you can clear your cache and disable Wi-Fi, and the problem instantly goes away. However, when you do that, you're using precious data.


2 Answers

PackageManager pm = context.getPackageManager(); ApplicationInfo appInfo = pm.getApplicationInfo("app.package.name", 0); String appFile = appInfo.sourceDir; long installed = new File(appFile).lastModified(); //Epoch Time 
like image 184
yanchenko Avatar answered Oct 10 '22 19:10

yanchenko


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; } 
like image 37
orip Avatar answered Oct 10 '22 21:10

orip