2011-03-15 87 views
49

有沒有辦法找到Android設備上的「安裝應用程序的日期」。在Android上獲取應用程序安裝日期

已經廣泛搜索,但無法找到相關答案。

通過PackageManager文檔/代碼安裝應用程序時,無法找到關於日期的任何信息。

非常感謝。 Mahim。

+2

請告訴,爲什麼你需要這個?知道第一次啓動日期還不夠嗎? – 2011-03-15 12:23:32

+1

這是根據客戶對我們正在進行的項目之一的要求。 – mahim 2011-03-17 07:43:36

+0

@VladimirIvanov「請告訴你爲什麼需要這個?」 http://stackoverflow.com/questions/19764667/determine-date-or-version-that-app-was-purchased-from-google-play-store-equi – 2015-07-28 10:14:44

回答

95

或者這一個:

long installed = context 
    .getPackageManager() 
    .getPackageInfo(context.getPackag‌​eName(), 0) 
    .firstInstallTime 
; 
+0

非常感謝您對這個主題的幫助。 – mahim 2011-03-17 07:42:44

+0

向上面的代碼添加一個try-catch塊,將足夠好 – 2013-09-05 05:29:46

+1

您將能夠通過此鏈接獲得所有其他「包信息」,http://developer.android.com/reference/android/content /pm/PackageInfo.html – fedmich 2014-01-09 22:59:40

22

使用此代碼:(!API等級9向上)

PackageManager pm = context.getPackageManager(); 
ApplicationInfo appInfo = pm.getApplicationInfo("app.package.name", 0); 
String appFile = appInfo.sourceDir; 
long installed = new File(appFile).lastModified(); 
+0

非常感謝Sunil,我們能夠繼續前進您上面的寶貴意見。 – mahim 2011-03-17 07:41:55

+23

每次更新包時,此處返回的時間都會更改。 – Jason 2011-09-27 22:13:45

7

嘗試這些

/** 
* The time at which the app was first installed. Units are as per currentTimeMillis(). 
* @param context 
* @return 
*/ 
public static long getAppFirstInstallTime(Context context){ 
    PackageInfo packageInfo; 
    try { 
    if(Build.VERSION.SDK_INT>8/*Build.VERSION_CODES.FROYO*/){ 
     packageInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0); 
     return packageInfo.firstInstallTime; 
    }else{ 
     //firstinstalltime unsupported return last update time not first install time 
     ApplicationInfo appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0); 
     String sAppFile = appInfo.sourceDir; 
     return new File(sAppFile).lastModified(); 
    } 
    } catch (NameNotFoundException e) { 
    //should never happen 
    return 0; 
    } 
} 
-1

這個方法返回一個以字符串格式安裝的日期,如12/25/2016 10:38:02

private String getInstallDate() { 
     // get app installation date 

     PackageManager packageManager = getActivity().getPackageManager(); 
     long installTimeInMilliseconds; // install time is conveniently provided in milliseconds 

     Date installDate = null; 
     String installDateString = null; 

     try { 
      PackageInfo packageInfo = packageManager.getPackageInfo(getActivity().getPackageName(), 0); 
      installTimeInMilliseconds = packageInfo.firstInstallTime; 
      installDateString = MiscUtilities.getDate(installTimeInMilliseconds, "MM/dd/yyyy hh:mm:ss"); 
     } 
     catch (PackageManager.NameNotFoundException e) { 
      // an error occurred, so display the Unix epoch 
      installDate = new Date(0); 
      installDateString = installDate.toString(); 
     } 

     return installDateString; 
    } 
相關問題