我想找出一个应用程序已经在设备上安装了多长时间。
原因:我想为很久以前安装了该应用程序的用户宣布该应用程序的新更新,并希望避免为刚刚安装该应用程序的用户宣布此更新。我讨厌自己在应用程序中投放广告:-),所以我想保持谨慎,只对很久以前安装了该应用程序的用户显示公告。(对不起,我的英语不好)
我不想使用的是以下内容,因为这使我只能从实施该功能的较新版本中检测到它:-使用Google Analytics(分析)。-一个计数器,用于计算属性中的appstarts
我正在寻找一种解决方案,该解决方案将于2014年实施,并检测到自2013年以来已安装了某个应用程序。
有什么提示吗?
进行此操作最简单的方法是使用一对SharedPreferences
:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(new Context());
boolean firstRun = prefs.getBoolean("IS_FIRST_RUN", true);
long now = System.currentTimeMillis();
if (firstRun)
{
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("IS_FIRST_RUN", false);
editor.putLong("FIRST_RUN_MILLIS", now);
editor.apply();
}
else
{
long dateFirstRun = prefs.getLong("FIRST_RUN_MILLIS", 0L);
if (dateFirstRun == 0L)
{
// no value saved. decide how you want to handle
}
else if (now - dateFirstRun < SOME_AGE)
{
// offer extra functionality
}
}
如果您想检查以前的安装,我认为您将必须使用PackageManager
编辑:正如理查德在上面的评论中建议的那样:
try
{
long firstInstall = context.getPackageManager().getPackageInfo("package.name", 0).firstInstallTime;
}
catch (PackageManager.NameNotFoundException e)
{
e.printStackTrace();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句