2010-05-14 50 views
28

我嘗試了一些方法,但沒有成功,幫助我。如何從android中獲取應用安裝時間

+18

你可以添加幾百個詞來解釋你所指的是什麼嗎? – CommonsWare 2010-05-14 01:35:52

+3

我想他想知道應用何時安裝。 – Macarse 2010-05-14 02:41:41

+0

誰告訴我,如何獲取安裝的List應用程序,它由用戶安裝,非系統。 – jezz 2010-05-14 07:22:10

回答

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

謝謝你,這是我想要的。 – jezz 2010-05-14 07:19:29

+5

此外,這應顯示上次安裝***或更新***時間。 – orip 2011-10-23 10:17:40

+6

實際首次安裝時間而不是更新時間如何? – 2012-08-28 05:15:52

47

在API級9(薑餅)以上,還有的PackageInfo.firstInstallTime字段,保持毫秒從epoch:

packageManager.getPackageInfo(packageName, 0).firstInstallTime; 

我有以下代碼(如果可用)來使用它,並回退到在APK修改時間:

// return install time from package manager, or apk file modification time, 
// or null if not found 
public Date getInstallTime(
    PackageManager packageManager, String packageName) { 
    return firstNonNull(
    installTimeFromPackageManager(packageManager, packageName), 
    apkUpdateTime(packageManager, packageName)); 
} 

private Date apkUpdateTime(
    PackageManager packageManager, String packageName) { 
    try { 
    ApplicationInfo info = packageManager.getApplicationInfo(packageName, 0); 
    File apkFile = new File(info.sourceDir); 
    return apkFile.exists() ? new Date(apkFile.lastModified()) : null; 
    } catch (NameNotFoundException e) { 
    return null; // package not found 
    } 
} 

private Date installTimeFromPackageManager(
    PackageManager packageManager, String packageName) { 
    // API level 9 and above have the "firstInstallTime" field. 
    // Check for it with reflection and return if present. 
    try { 
    PackageInfo info = packageManager.getPackageInfo(packageName, 0); 
    Field field = PackageInfo.class.getField("firstInstallTime"); 
    long timestamp = field.getLong(info); 
    return new Date(timestamp); 
    } catch (NameNotFoundException e) {   
    return null; // package not found 
    } catch (IllegalAccessException e) { 
    } catch (NoSuchFieldException e) { 
    } catch (IllegalArgumentException e) { 
    } catch (SecurityException e) { 
    } 
    // field wasn't found 
    return null; 
} 

private Date firstNonNull(Date... dates) { 
    for (Date date : dates) 
    if (date != null) 
     return date; 
    return null; 
} 
10

PackageInfo.firstInstallTime讓你在「Unix時間」安裝時間(時間我自「時代」,即1970年1月1日00:00:00 UTC)以來的毫秒數。您可以使用java.util.Datejava.text.DateFormat來格式化這個時間。

private static final String TAG = "MyActivity"; 

... 
packageName = ... 
... 

try { 
    PackageInfo packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS); 

    Date installTime = new Date(packageInfo.firstInstallTime); 
    Log.d(TAG, "Installed: " + installTime.toString()); 

    Date updateTime = new Date(packageInfo.lastUpdateTime); 
    Log.d(TAG, "Updated: " + updateTime.toString()); 
} 
catch (PackageManager.NameNotFoundException e) { 
    e.printStackTrace(); 
} 

您還可以用java.text.SimpleDateFormat更改日期格式。

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 

String installTime = dateFormat.format(new Date(packageInfo.firstInstallTime)); 
Log.d(TAG, "Installed: " + installTime); 

String updateTime = dateFormat.format(new Date(packageInfo.lastUpdateTime)); 
Log.d(TAG, "Updated: " + updateTime); 
+1

謝謝bro,工作正常。 – vinidog 2016-02-11 00:39:36

+1

如果你只需要那些2次(只是傳遞「0」),它不需要從包管理器請求「PackageManager.GET_PERMISSIONS」 – for3st 2016-12-18 11:43:01

相關問題