简体   繁体   中英

Android Java: Find out if user is new to the app or if the app was previously installed

I'd like to publish a free Android app which is monetized by ads. I do not want to show ads within the first 24 hours, because if the user doesn't like the app, why should he/she be bothered by ads. If he likes the app and keeps it, there will be ads or a pro-version.

How could I find out if "now" is within those first 24 hours of app usage (could start from the time the app was installed or the time the app was first opened, that doesn't matter). I don't want the user to be able to just uninstall the app, then reinstall it and suddenly there are no ads anymore.

Is there some kind of a unique ID that does not change and that can be used to be looked up in a user database (Device-ID, Firebase-ID, ...)? Is there any better way to achieve this behavior?

Thank you!

There is unique id for every Android device.

Please check this post

Unique ID of Android device

I'm trying to implement the same feature for my own app right now, and I am going to try this approach first, where I save the timestamp of the first app opened to sharadpreferences. I'm going to do it this way because I don't want to use cloud databases and device ID:s right now. The only drawback would be, information of first app opened would disappear when app is uninstalled. However, I think that would not be very big issue for me at least.

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

}

and after this I would check within the showAd function that the user has used the app long enough. You can use the SimpleDateFormat for easier use of the date objects and calculate the time between current time and userLoginTime when you want to show the ad.

//time between two Date objects. Unit mils
Long diff = userLoginTime.getTime() - currentTime.getTime()

to make this more "efficient" you can save adsEnabled boolean to SharedPreferences after the time you choose. Then you only need to check if adsEnabled is true.

I know that this will disappear when user deletes and reinstalls the app, but I think that few user will actually do that. As you mentioned, if they like the app, they would not mind the ads (if the ads are cleverly placed). Moreover, in my opinion, this approach is much easier to implement and it almost do everything you want, so it would be worth of a try.

-Jussi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM