Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out if a specific Android app was previously installed

Tags:

android

I have an App which provides you a list of various apps that you can download and install from Play Store to earn goodies. Now, I don't want a user to uninstall a previously installed app and download it again through my app and earn goodies.

Is there a way to find out if a specific app was previously installed on the user's device?

Update I am interested in all the apps installed/uninstalled, regardless of when my app was installed. I don't want to store any data about any app installs on my end, I want to know if this data is already stored on the device somewhere for further reference.

Note I am also interested in the apps that we uninstalled. Can I get this data too?

like image 773
patrickfdsouza Avatar asked Jul 14 '14 11:07

patrickfdsouza


1 Answers

I can see where you are trying to go, but I think you need to rethink the approach a bit. You should always allow your users to install and uninstall as they wish. But you can put a check in the app to see when the app was first installed.

PackageInfo info = pm.getPackageInfo(packageName, 0);
long firstInstallTime = info.firstInstallTime;

This will store the time the app was first installed in firstInstallTime This time stamp will not change with any number of subsequent uninstalls and re-installs.

The PackageInfo class provides a bunch of other useful info about your app on the device and is well worth getting to know.

You can compare this to the timestamp when the apps source directory was last modified (in other words when the app was most recent installed), which you can obtain with:

ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(packageName, 0);
long mostRecentInstallTime = new File(appInfo.sourceDir).lastModified();

I've also used this approach to give users a 1 week trial of an apps full features before reverting to the lesser "free mode", and they can't trip it up by uninstalling and re-installing.


Additional:
In response to your comment...
You are not just restricted to getting PackageInfo for your own app or apps installed after yours was. You can get PackageInfo for all apps currently on the device, regardless of when they were installed in the "devices lifetime".
This slightly modified version of the code found here will give you the firstInstallTime for all apps on the device:

// Get PackageInfo for each app on the device
List<PackageInfo> packageInfoInstalledPackages = getPackageManager().getInstalledPackages(0);

// Now iterate through to get the info you need.
long[] firstInstallTimes = long[packageInfoInstalledPackages.size()];
for(int i=0;i<packageInfoInstalledPackages.size();i++) {
    PackageInfo p = packageInfoInstalledPackages.get(i);
    if (p.versionName != null) {
        firstInstallTimes[i] = p.firstInstallTime;
    }        
}

You can also get the ApplicationInfo so you should have all you need.

like image 134
Sound Conception Avatar answered Oct 07 '22 03:10

Sound Conception