簡體   English   中英

有沒有辦法找出一個應用程序安裝了多長時間?

[英]Is there a way to find out how long an app is installed?

我想找出一個應用程序已經在設備上安裝了多長時間。

原因:我想為很久以前安裝了該應用程序的用戶宣布該應用程序的新更新,並希望防止為剛剛安裝該應用程序的用戶宣布此更新。 我討厭自己在應用程序中投放廣告:-),所以我想保持謹慎,只對很久以前安裝了該應用程序的用戶顯示公告。 (對不起,我的英語不好)

我不想使用的是以下內容,因為這使我只能從實施該功能的較新版本中檢測到它:-使用Google Analytics(分析)。 -一個計數器,用於計算屬性中的appstarts

我正在尋找一種解決方案,該解決方案將於2014年實施,並檢測到自2013年以來已安裝了某個應用。

有什么提示嗎?

進行此操作最簡單的方法是使用一對SharedPreferences

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(new Context());
boolean firstRun = prefs.getBoolean("IS_FIRST_RUN", true);
long now = System.currentTimeMillis();
if (firstRun)
{
    SharedPreferences.Editor editor = prefs.edit();
    editor.putBoolean("IS_FIRST_RUN", false);
    editor.putLong("FIRST_RUN_MILLIS", now);
    editor.apply();
}
else
{
    long dateFirstRun = prefs.getLong("FIRST_RUN_MILLIS", 0L);
    if (dateFirstRun == 0L)
    {
        // no value saved. decide how you want to handle
    }
    else if (now - dateFirstRun < SOME_AGE)
    {
        // offer extra functionality
    }
}

如果您想檢查以前的安裝,我認為您必須使用PackageManager

編輯:正如理查德在上面的評論中建議的那樣:

try
{
    long firstInstall = context.getPackageManager().getPackageInfo("package.name", 0).firstInstallTime;
}
catch (PackageManager.NameNotFoundException e)
{
    e.printStackTrace();
}

以我的經驗,如果該應用程序在Google Play上運行,我會在個人服務器上使用UrbanAirship,Parse或原始GCM實施等服務來注冊設備,並能夠向每部手機發送推送消息。 我不介意已經安裝的應用程序,因為設備上的Play商店應用程序會在可用時向用戶發布有關更新的信息。

現在,如果該應用程序不在Google Play上,但仍在使用api,則可以利用它並發送一次促銷信息,以下載具有新功能的新版本:

    //TODO Receive message here

    //check preferences
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    if(!prefs.getBoolean("alreadyAdvertised", false)){
        prefs.edit().putBoolean("alreadyAdvertised", true).commit();
        //TODO display message here

    }

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM