且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

找出以前是否安装了特定的Android应用程序

更新时间:2023-01-01 23:18:00

我可以看到您要去的地方,但是我认为您需要重新考虑一下这种方法.您应该始终允许用户根据需要安装和卸载.但是您可以在应用程序中进行检查,以查看该应用程序的首次安装时间.

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;

这将在 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.

PackageInfo 类提供了许多其他有用的信息关于您设备上的应用程序的信息,非常值得了解.

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

我还使用这种方法为用户提供了为期1周的应用全功能试用版,然后又恢复到较小的免费模式",并且他们无法通过卸载和重新安装来启动它.

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.

附加:
回应您的评论...
您不仅限于为自己的应用或在安装之后安装的应用获取PackageInfo.您可以获取设备上当前所有应用程序的PackageInfo,无论它们何时在设备生存期"中安装.
此处找到此代码的经过稍微修改的版本将为您提供设备上所有应用程序的firstInstallTime:

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;
    }        
}

您还可以获取 ApplicationInfo ,因此您应该拥有所需的一切.

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