Find out if a specific Android app was previously installed

前端 未结 2 1839
时光说笑
时光说笑 2021-02-10 10:45

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

相关标签:
2条回答
  • 2021-02-10 11:24

    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.

    0 讨论(0)
  • 2021-02-10 11:35

    This snippet will log all the main activities of the apps installed on you device, you just have to check that list for a given app

    final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    final List <ResolveInfo> pkgAppsList = getPackageManager().queryIntentActivities( mainIntent, 0);
    
    for(ResolveInfo resolve : pkgAppsList)
    {
        Log.d("MY_APP", resolve.toString());
    }
    

    Hope it helps!

    0 讨论(0)
提交回复
热议问题