Example usage for android.content.pm PackageManager getPackageInfo

List of usage examples for android.content.pm PackageManager getPackageInfo

Introduction

In this page you can find the example usage for android.content.pm PackageManager getPackageInfo.

Prototype

public abstract PackageInfo getPackageInfo(VersionedPackage versionedPackage, @PackageInfoFlags int flags)
        throws NameNotFoundException;

Source Link

Document

Retrieve overall information about an application package that is installed on the system.

Usage

From source file:Main.java

private static int b(Context paramContext, int paramInt) {
    PackageManager localPackageManager = paramContext.getPackageManager();
    int i;/*from www.  j a va  2 s  .com*/
    try {
        PackageInfo localPackageInfo = localPackageManager.getPackageInfo("com.google.android.apps.plus", 0);
        ApplicationInfo localApplicationInfo = localPackageManager
                .getApplicationInfo("com.google.android.apps.plus", 0);
        if (localPackageInfo.versionCode < paramInt)
            return 2;
        boolean bool = localApplicationInfo.enabled;
        i = 0;
        if (!bool)
            return 3;
    } catch (PackageManager.NameNotFoundException localNameNotFoundException) {
        i = 1;
    }
    return i;
}

From source file:Main.java

/**
 * Retrieves the application version as defined in the manifest.
 *
 * @param context {@link Context} used to retrieve the {@link PackageManager}.
 *
 * @return application version/* w  w  w. j  av  a  2  s.  c  o  m*/
 */
public static String getApplicationVersion(final Context context) {
    String version = "1.0";

    try {
        final PackageInfo info;
        final PackageManager manager = context.getPackageManager();

        if (manager != null) {
            info = manager.getPackageInfo(context.getPackageName(), 0);

            version = info.versionName;
        }
    } catch (final NameNotFoundException ex) {
        version = "1.0";
    }

    return version;
}

From source file:Main.java

/**
 * Checks if the application is installed on the SD card. See
 * http://stackoverflow.com/questions///from   w w w . j av  a 2 s  .  c om
 * 5814474/how-can-i-find-out-if-my-app-is-installed-on-sd-card
 *
 * @return <code>true</code> if the application is installed on the sd card
 */
@SuppressLint("SdCardPath")
public static boolean isInstalledOnSdCard(Context context) {
    // check for API level 8 and higher
    if (VERSION.SDK_INT > android.os.Build.VERSION_CODES.ECLAIR_MR1) {
        PackageManager pm = context.getPackageManager();
        try {
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            ApplicationInfo ai = pi.applicationInfo;
            return (ai.flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == ApplicationInfo.FLAG_EXTERNAL_STORAGE;
        } catch (NameNotFoundException e) {
            // ignore
        }
    }

    // check for API level 7 (rooted devices) - check files dir
    try {
        String filesDir = context.getFilesDir().getAbsolutePath();
        if (filesDir.startsWith("/data/")) {
            return false;
        } else if (filesDir.contains("/mnt/") || filesDir.contains("/sdcard/")) {
            return true;
        }
    } catch (Throwable e) {
        // ignore
    }

    return false;
}

From source file:com.gotraveling.insthub.BeeFramework.Utils.Utils.java

public static String getAppVersionName(Context context) {
    String versionName = "";
    try {/*from w ww.j  a va 2  s.  com*/
        // ---get the package info---
        PackageManager pm = context.getPackageManager();
        PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
        versionName = pi.versionName;
        if (versionName == null || versionName.length() <= 0) {
            return "";
        }
    } catch (Exception e) {
    }
    return versionName;
}

From source file:Main.java

public static String getAppVersion(PackageManager packageManager, String packageName) {
    String version = "?";
    try {//  w  ww.j a v  a 2  s  . c o m
        if (packageManager != null)
            version = packageManager.getPackageInfo(packageName, 0).versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return version;
}

From source file:Main.java

/**
 * Retrieves the PackageInfo object for this application.
 *
 * @param context Any context./*from  w  ww  . j  a v a 2  s .com*/
 * @return The PackageInfo object for this application.
 */
public static PackageInfo getOwnPackageInfo(Context context) {
    PackageManager manager = context.getPackageManager();
    try {
        String packageName = context.getApplicationContext().getPackageName();
        return manager.getPackageInfo(packageName, 0);
    } catch (NameNotFoundException e) {
        // Should never happen.
        throw new AssertionError("Failed to retrieve own package info");
    }
}

From source file:Main.java

/**
 * get app version code//from w  ww  .  j  a v a 2s.  com
 *
 * @param context
 * @return
 */
public static String getAppVersionName(Context context) {
    if (context != null) {
        PackageManager pm = context.getPackageManager();
        if (pm != null) {
            PackageInfo pi;
            try {
                pi = pm.getPackageInfo(context.getPackageName(), 0);
                if (pi != null) {
                    return pi.versionName;
                }
            } catch (PackageManager.NameNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
    return "";
}

From source file:Main.java

/**
 * get app version code/*  ww w.  ja  v  a  2s  . c om*/
 * 
 * @param context
 * @return
 */
public static int getAppVersionCode(Context context) {
    if (context != null) {
        PackageManager pm = context.getPackageManager();
        if (pm != null) {
            PackageInfo pi;
            try {
                pi = pm.getPackageInfo(context.getPackageName(), 0);
                if (pi != null) {
                    return pi.versionCode;
                }
            } catch (NameNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
    return -1;
}

From source file:Main.java

/**
 * Check if a certain application is installed on a device.
 *
 * @param context the applications context.
 * @param packageName the package name that you want to check.
 *
 * @return true if the application is installed, false otherwise.
 *//*w  ww  .  j a  v a 2s .  co  m*/
public static boolean isApplicationInstalled(Context context, String packageName) {
    PackageManager pm = context.getPackageManager();
    try {
        // Check if the package name exists, if exception is thrown, package name does not exist.
        pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

From source file:Main.java

public static long getFirstInstalled() {
    long firstInstalled = 0;
    PackageManager pm = myApp.getPackageManager();
    try {//from  www . j av a2 s. com
        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;
}