检测是新安装还是更新版本(Android应用) [英] Detect if new install or updated version (Android app)

查看:360
本文介绍了检测是新安装还是更新版本(Android应用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Play商店中有一个应用.我要提出一个要求,如果用户要使用应用程序的特定部分,则必须先邀请朋友才能使用.但我只想将此限制强加给新安装的应用程序(对在限制之前已安装了该应用程序的用户来说是公平的). 很长的介绍很抱歉,我的问题是如何确定当前设备是否已更新该应用程序或是新安装?

I have an app on the Play Store. I want to put a requirement that if users want to use a certain part of the app, they have to invite a friend before being able to do so. But I only want to impose this restriction to new installs of the app (to be fair to users that have installed the app before the restriction). Sorry for the long intro, my question is how can I find out if the current device has updated the app or is a new install?

推荐答案

public static boolean isFirstInstall() {
    try {
        long firstInstallTime =   App.getContext().getPackageManager().getPackageInfo(getPackageName(), 0).firstInstallTime;
        long lastUpdateTime = App.getContext().getPackageManager().getPackageInfo(getPackageName(), 0).lastUpdateTime;
        return firstInstallTime == lastUpdateTime;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return true;
    }
}



public static boolean isInstallFromUpdate() {
    try {
        long firstInstallTime =   App.getContext().getPackageManager().getPackageInfo(getPackageName(), 0).firstInstallTime;
        long lastUpdateTime = App.getContext().getPackageManager().getPackageInfo(getPackageName(), 0).lastUpdateTime;
        return firstInstallTime != lastUpdateTime;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return false;
    }
}

这篇关于检测是新安装还是更新版本(Android应用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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