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

public static String getSDKVersion(Context context) {
    PackageManager pm = context.getPackageManager();
    String pkgName = context.getPackageName();
    PackageInfo pkgInfo = null;/*www.  ja  va2  s.  co  m*/
    String ret = "";
    try {
        pkgInfo = pm.getPackageInfo(pkgName, PackageManager.GET_CONFIGURATIONS);
        ret = pkgInfo.versionName;
    } catch (NameNotFoundException ex) {

    }
    return ret;
}

From source file:Main.java

public static PackageInfo getPackageInfo(Context context) {
    // Get packagemanager
    PackageManager packageManager = context.getPackageManager();
    // 0 means get version info
    PackageInfo packInfo = null;/*ww w  . j  av a 2  s  .c  om*/
    try {
        packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);

    } catch (NameNotFoundException e) {
        // mLogger.w("get version failed");
    }
    return packInfo;
}

From source file:Main.java

public static String getSignature(Context context) {
    PackageManager pm = context.getPackageManager();
    PackageInfo pi = null;//from   ww w .  j  a  v a 2  s. c  o m
    StringBuilder builder = new StringBuilder();
    try {
        pi = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_SIGNATURES);
        Signature[] signatures = pi.signatures;
        for (Signature signature : signatures) {
            builder.append(getMessageDigest(signature.toByteArray()));
        }
        return builder.toString();
    } catch (Exception e1) {
        e1.printStackTrace();
        return "";
    }
}

From source file:Main.java

/**
 * Get give application status: installed, not installed or update.
 * /*from ww w  .  j  av a 2 s  . c  om*/
 * @param context Object of {@link Context}.
 * @param pkgName Package name of app.
 * @param code Application version code.
 * @return {@link #STATUS_INSTALLED}, {@link #STATUS_NOT_INSTALLED}
 *    or {@link #STATUS_UPDATED}.
 */
public final static int getAppStatus(Context context, String pkgName, int code) {
    if (null == pkgName || null == context) {
        return STATUS_NOT_INSTALLED;
    }

    PackageInfo pkgInfo = null;
    PackageManager pm = context.getPackageManager();

    if (null == pm) {
        return STATUS_NOT_INSTALLED;
    }

    try {
        pkgInfo = pm.getPackageInfo(pkgName, 0);
        if (null == pkgInfo) {
            return STATUS_NOT_INSTALLED;
        } else {
            if (pkgInfo.versionCode == code) {
                return STATUS_INSTALLED;
            } else if (code > pkgInfo.versionCode) {
                return STATUS_UPDATED;
            } else {
                return STATUS_INSTALLED;
            }
        }
    } catch (Exception e) {
        return STATUS_NOT_INSTALLED;
    }
}

From source file:at.ac.uniklu.mobile.sportal.Studentportal.java

/**
 * Tells if the application has been built in debug mode.
 * code taken and adapted from: http://stackoverflow.com/questions/3029819/android-automatically-choose-debug-release-maps-api-key
 * @param context/*from   w  w  w  . ja  v  a 2  s  .  com*/
 * @return
 */
private static boolean isDebugBuild(Context context) {
    try {
        PackageManager pm = context.getPackageManager();
        PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
        return ((pi.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0);
    } catch (Exception e) {
    }
    return false;
}

From source file:com.fallenangelsguild.eltime.WebHelper.java

/**
 * Prepare the internal User-Agent string for use. This requires a
 * {@link Context} to pull the package name and version number for this
 * application.//from  w  w  w  .ja  v  a  2 s.com
 */
public static void initialize(Context context) {
    c = context;
    try {
        // Read package name and version number from manifest
        PackageManager manager = c.getPackageManager();
        PackageInfo info = manager.getPackageInfo(c.getPackageName(), 0);
        sUserAgent = String.format(c.getString(R.string.template_user_agent), info.packageName,
                info.versionName);

    } catch (NameNotFoundException e) {
        Log.e(TAG, "Couldn't find package information in PackageManager", e);
    }
}

From source file:minghai.nisesakura.SimpleBottleHelper.java

/**
 * Prepare the internal User-Agent string for use. This requires a
 * {@link Context} to pull the package name and version number for this
 * application.//from  w  ww .ja  v a  2  s .  c o  m
 */
public static void prepareUserAgent(Context context) {
    try {
        // Read package name and version number from manifest
        PackageManager manager = context.getPackageManager();
        PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
        sUserAgent = String.format(context.getString(R.string.template_user_agent), info.packageName,
                info.versionName);
        Log.d(TAG, "sUserAgent = " + sUserAgent);

    } catch (NameNotFoundException e) {
        Log.e(TAG, "Couldn't find package information in PackageManager", e);
    }
}

From source file:org.metasyntactic.utilities.ExceptionUtilities.java

public static void registerExceptionHandler(Context context) {
    PackageManager pm = context.getPackageManager();
    try {//from  w  w w  .  j  a va 2s .c o m
        PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
        Version = pi.versionName;
        Package = pi.packageName;
        FilesPath = context.getFilesDir().getAbsolutePath();
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }

    new Thread() {
        @Override
        public void run() {
            submitStackTraces();
            Thread.setDefaultUncaughtExceptionHandler(DEFAULT_EXCEPTION_HANDLER);
        }
    }.start();
}

From source file:com.joelapenna.foursquared.appwidget.stats.FoursquareHelper.java

/**
 * Prepare the internal User-Agent string for use. This requires a
 * {@link android.content.Context} to pull the package name and version number for this
 * application./*from   w  w  w  . ja va 2  s  .  co  m*/
 */
public static void prepareUserAgent(Context context) {
    try {
        // Read package name and version number from manifest
        PackageManager manager = context.getPackageManager();
        PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
        sUserAgent = String.format(context.getString(R.string.stats_widget_template_user_agent),
                info.versionName, info.packageName);
    } catch (NameNotFoundException e) {
        Log.e(TAG, "Couldn't find package information in PackageManager", e);
    }
}

From source file:Main.java

public static Drawable getIconFromPackageName(String packageName, Context context) {
    PackageManager pm = context.getPackageManager();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        try {/*from w  w w  .  j  a  v a 2s  . c  o m*/
            PackageInfo pi = pm.getPackageInfo(packageName, 0);
            Context otherAppCtx = context.createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY);

            int displayMetrics[] = { DisplayMetrics.DENSITY_XXXHIGH, DisplayMetrics.DENSITY_XXHIGH,
                    DisplayMetrics.DENSITY_XHIGH, DisplayMetrics.DENSITY_HIGH, DisplayMetrics.DENSITY_MEDIUM,
                    DisplayMetrics.DENSITY_TV };

            for (int displayMetric : displayMetrics) {
                try {
                    Drawable d = otherAppCtx.getResources().getDrawableForDensity(pi.applicationInfo.icon,
                            displayMetric);
                    if (d != null) {

                        return d;
                    }
                } catch (Resources.NotFoundException e) {
                }
            }

        } catch (Exception e) {
            // Handle Error here
        }
    }

    ApplicationInfo appInfo = null;
    try {
        appInfo = pm.getApplicationInfo(packageName, PackageManager.GET_META_DATA);
    } catch (PackageManager.NameNotFoundException e) {
        return null;
    }

    return appInfo.loadIcon(pm);
}