[英]Android Java: Find out if user is new to the app or if the app was previously installed
我想发布一个免费的 Android 应用程序,该应用程序通过广告获利。 我不想在前 24 小时内展示广告,因为如果用户不喜欢该应用程序,他/她为什么要被广告打扰。 如果他喜欢该应用并保留它,就会有广告或专业版。
我怎样才能知道“现在”是否在应用程序使用的前 24 小时内(可以从应用程序安装时间或应用程序首次打开时间开始,没关系)。 我不希望用户能够只是卸载应用程序,然后重新安装它,突然就没有广告了。
是否存在某种不会更改且可用于在用户数据库中查找的唯一 ID(Device-ID、Firebase-ID、...)? 有没有更好的方法来实现这种行为?
谢谢!
我现在正在尝试为我自己的应用程序实现相同的功能,我将首先尝试这种方法,将第一个打开的应用程序的时间戳保存到 sharadpreferences。 我打算这样做是因为我现在不想使用云数据库和设备 ID:s。 唯一的缺点是,卸载应用程序时,第一个打开的应用程序的信息会消失。 但是,我认为这至少对我来说不是什么大问题。
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
boolean userLoginTime = sharedPreferences.contains(getString(R.string.userLoginTime));
if (!userLoginTime){
//save the current time to SharedPreferences if the field userLoginTime is missing
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date currentTime = Calendar.getInstance().getTime();
String stringCurrentTime = dateFormat.format(currentTime);//parser need try and catch. These are only for getting the idea
sharedPreferences.edit().putString(getString(R.string.userLoginTime),stringCurrentTime).apply();
}
在此之后,我会在 showAd function 中检查用户使用该应用程序的时间是否足够长。 您可以使用 SimpleDateFormat 来更轻松地使用日期对象,并在您想要展示广告时计算当前时间和 userLoginTime 之间的时间。
//time between two Date objects. Unit mils
Long diff = userLoginTime.getTime() - currentTime.getTime()
为了使这更“高效”,您可以在您选择的时间后将 adsEnabled boolean 保存到 SharedPreferences 中。 然后你只需要检查 adsEnabled 是否为真。
我知道当用户删除并重新安装应用程序时这会消失,但我认为很少有用户会真正这样做。 正如您所提到的,如果他们喜欢该应用程序,他们不会介意广告(如果广告被巧妙地放置)。 此外,在我看来,这种方法更容易实现,几乎可以满足您的所有需求,因此值得一试。
-尤西
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.