找出以前是否安装了特定的Android应用程序 [英] Find out if a specific Android app was previously installed

查看:83
本文介绍了找出以前是否安装了特定的Android应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,可为您提供各种应用程序的列表,您可以从Play商店下载并安装这些应用程序以赚钱.现在,我不希望用户卸载以前安装的应用并通过我的应用再次下载来赚钱.

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.

是否可以找到以前是否在用户设备上安装了特定应用的方法?

更新我对所有已安装/未安装的应用程序都感兴趣,无论何时安装我的应用程序.我不想在最后存储有关任何应用程序安装的任何数据,我想知道这些数据是否已经存储在设备上的某个地方,以供进一步参考.

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?

推荐答案

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

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.

这篇关于找出以前是否安装了特定的Android应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆