Java tutorial
//package com.java2s; /** * Copyright (C) 2012 Hyperionics Technology LLC <http://www.hyperionics.com> * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import android.app.Application; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import java.io.*; import java.lang.reflect.Method; public class Main { private static Application myApp = getApp(); public static boolean setApp(Application app) { myApp = app; getFirstInstalled(); // to initialize timestamp file on old versions of Android return true; } public static long getFirstInstalled() { long firstInstalled = 0; PackageManager pm = myApp.getPackageManager(); try { PackageInfo pi = pm.getPackageInfo(myApp.getApplicationContext().getPackageName(), pm.GET_SIGNATURES); try { try { //noinspection AndroidLintNewApi firstInstalled = pi.firstInstallTime; } catch (NoSuchFieldError e) { } } catch (Exception ee) { } if (firstInstalled == 0) { // old versions of Android don't have firstInstallTime in PackageInfo File dir; try { dir = new File( getApp().getApplicationContext().getExternalCacheDir().getAbsolutePath() + "/.config"); } catch (Exception e) { dir = null; } if (dir != null && (dir.exists() || dir.mkdirs())) { File fTimeStamp = new File(dir.getAbsolutePath() + "/.myconfig"); if (fTimeStamp.exists()) { firstInstalled = fTimeStamp.lastModified(); } else { // create this file - to make it slightly more confusing, write the signature there OutputStream out; try { out = new FileOutputStream(fTimeStamp); out.write(pi.signatures[0].toByteArray()); out.flush(); out.close(); } catch (Exception e) { } } } } } catch (PackageManager.NameNotFoundException e) { } return firstInstalled; } public static Application getApp() { if (myApp == null) { try { final Class<?> activityThreadClass = Class.forName("android.app.ActivityThread"); final Method method = activityThreadClass.getMethod("currentApplication"); myApp = (Application) method.invoke(null, (Object[]) null); } catch (Exception e) { // handle exception } } // below gives me /mnt/sdcard/Android/data/com.hyperionics.pdfxTest/cache/tmpPdfx // File file = new File(app.getApplicationContext().getExternalCacheDir(), "tmpPdfx"); return myApp; } }