简体   繁体   中英

Is there a way to find out how long an app is installed?

I would like to find out how long an app is already installed on a device.

The reason : I like to announce a new update for the app for users who have installed the app longtime ago and want to prevent to announce this for users, which have just installed the app. I hate myself advertising in the apps :-) so I want to be discreet and show the announcement for only users which have installed the app longtime ago. (Sorry for my bad english)

what i do not want to use is this the following, because this allow me to detect this only from a newer version, where I implemented it: - using google analytics. - a counter which counts the appstarts in a property

I am looking for a solution that I implement now in 2014 and detects that an app is installed since 2013.

any hints ?

The easiest way to do this moving forward would be with a pair of 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
    }
}

If you want to check past installs I think you will have to play with the PackageManager

Edit: As Richard suggests in the comments above:

try
{
    long firstInstall = context.getPackageManager().getPackageInfo("package.name", 0).firstInstallTime;
}
catch (PackageManager.NameNotFoundException e)
{
    e.printStackTrace();
}

In my experience, if the app is on Google Play, I would use some service like UrbanAirship, Parse, or raw GCM implementation with personal server to register devices and be able to send push messages to every handset. I wouldn't mind about the already installed apps 'cause the Play Store app on the device will announce the user about the update when available.

Now, if the app is not on Google Play, but still consumes an api, you can take advantage of it and send a one-time promotional message to download the new version with the new features:

    //TODO Receive message here

    //check preferences
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    if(!prefs.getBoolean("alreadyAdvertised", false)){
        prefs.edit().putBoolean("alreadyAdvertised", true).commit();
        //TODO display message here

    }

Hope it helps.

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