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 android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;

import android.content.pm.PackageManager.NameNotFoundException;

import java.io.File;

public class Main {
    /**
     * To check if the app has opened ever(most time).
     * 
     * @param  context     context
     * @param  packageName package name
     * @return             true if the app has actived, otherwise return false
     */
    public static boolean isAppActivated(Context context, String packageName) {
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
            if (packageInfo == null) {
                return false;
            }

            ApplicationInfo appInfo = packageInfo.applicationInfo;
            File file = new File(appInfo.dataDir);

            /* if the /data/data/<packagename> is not existed, return false */
            if (file == null || !file.exists()) {
                return false;
            }

            long lastUpdateTime = packageInfo.lastUpdateTime;
            long lastModifiedTime = file.lastModified();

            /* if the delta time is greater than 1.5s(most time), then the app has activated */
            if (Math.abs(lastModifiedTime - lastUpdateTime) >= 1500) {
                return true;
            }

            return false;
        } catch (NameNotFoundException e) {
            return false;
        }
    }
}