Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.io.File;

import android.content.Context;

import android.content.pm.ApplicationInfo;

import android.content.pm.PackageManager;

public class Main {
    /**
     * Get give package app install date.
     * 
     * @param context Object of {@link Context}.
     * @param pkg Package name.
     * @return Measured in milliseconds since January 1st, 1970, midnight. Returns 0 if the file does not exist.
     */
    public final static long getAppInstallDate(Context context, String pkg) {
        if (null == context || null == pkg) {
            return System.currentTimeMillis();
        }

        PackageManager pm = context.getPackageManager();
        try {
            ApplicationInfo info = pm.getApplicationInfo(pkg, 0);

            // 2.3(API level 9) ApplicationInfo have a method to get install time.
            // but we can't use it. (for support 2.2 -_-||).
            File file = new File(info.sourceDir);
            return file.lastModified();

        } catch (Exception e) {
            e.printStackTrace();
            return System.currentTimeMillis();
        }
    }
}