如何从android获得应用程序安装时间

我尝试一些方法,但不成功,帮助我。

3 Solutions collect form web for “如何从android获得应用程序安装时间”

PackageManager pm = context.getPackageManager(); ApplicationInfo appInfo = pm.getApplicationInfo("app.package.name", 0); String appFile = appInfo.sourceDir; long installed = new File(appFile).lastModified(); //Epoch Time 

在API级别9(姜饼)和以上,有PackageInfo.firstInstallTime字段,自纪元以来持有毫秒:

 packageManager.getPackageInfo(packageName, 0).firstInstallTime; 

我有下面的代码来使用它,如果可用,并回落到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; } 

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(); } 

您也可以使用java.text.SimpleDateFormat来更改date格式。

 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); 
  • 在Android中的标签中启动活动
  • 为什么Android Studio报告“URI未注册”?
  • 通过促销代码进行的应用内购买会返回空的开发人员负载string
  • google-services.json为不同的productFlavors
  • 为什么我们使用canvas.save或canvas.restore?
  • 如何testingandroid引荐跟踪?
  • 如何在Eclipse中构buildAPK文件?
  • 在Android Studio中导入Facebook库:无法find属性“ANDROID_BUILD_SDK_VERSION”
  • Logcat的大小限制是多less,如何改变它的容量?
  • Environment.getExternalStorageDirectory不会返回可移动存储的path
  • Android Studio说“无法parsing符号”,但项目编译