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 getVersionName(Context context) {
    PackageManager p = context.getPackageManager();
    // GetPackageName () is your current class package name, 0 stands for is
    // to get version information
    PackageInfo packInfo;/*from   w w w . java2  s  .co m*/
    try {
        packInfo = p.getPackageInfo(context.getPackageName(), 0);
        return packInfo.versionName;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:net.peterkuterna.android.apps.devoxxfrsched.util.HttpUtils.java

/**
 * Build and return a user-agent string that can identify this application
 * to remote servers. Contains the package name and version code.
 *//*  w  w w  .ja v a 2s.co m*/
public static String buildUserAgent(Context context) {
    try {
        final PackageManager manager = context.getPackageManager();
        final PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);

        // Some APIs require "(gzip)" in the user-agent string.
        return info.packageName + "/" + info.versionName + " (" + info.versionCode + ") (gzip)";
    } catch (NameNotFoundException e) {
        return null;
    }
}

From source file:Main.java

public static int getVersionCode(Context context) {
    PackageManager p = context.getPackageManager();
    // GetPackageName () is your current class package name, 0 stands for is
    // to get version information
    PackageInfo packInfo;/* w  ww . ja  va2  s.c o m*/
    try {
        packInfo = p.getPackageInfo(context.getPackageName(), 0);

        return packInfo.versionCode;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
        return 0;
    }
}

From source file:Main.java

/**
 * get all <uses-permission> tags included under <manifest>
 * //from  www.j a  v  a  2 s . c  om
 * @param context
 */
public static void getAllAppPermissions(Context context) {
    PackageManager pm = context.getPackageManager();
    PackageInfo packageInfo;
    try {
        // Array of all <uses-permission> tags included under <manifest>, or
        // null if there were none.
        packageInfo = pm.getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS);
        String permissions[] = packageInfo.requestedPermissions;

        if (permissions != null) { // to list permission
            for (String permission : permissions) {
                Log.i("4A", "permission:" + permission);
            }
        }

    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
}

From source file:com.bw.hawksword.wiktionary.SimpleWikiHelper.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./* w  ww.  j  a  va  2s .  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);

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

From source file:Main.java

/**
 * Gets the Package Name/*ww w  . j  a v  a 2 s .  c  o m*/
 * 
 * @return
 */
public static String getPackageName(Context context) {
    PackageManager p = context.getPackageManager();
    // GetPackageName () is your current class package name, 0 stands for is
    // to get version information
    PackageInfo packInfo;
    try {
        packInfo = p.getPackageInfo(context.getPackageName(), 0);
        return packInfo.packageName;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:com.balk.rsswidget.GoogleReaderHelper.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 .  j a v  a 2 s .com
 */
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_wotd_title), info.packageName,
                info.versionName);

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

From source file:org.linuxguy.HPCCMonWidget.Helper.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.// ww w.j  a  v a 2s.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 = "HPCCMonWidget/Android";

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

From source file:com.hybris.mobile.logging.ExceptionHandler.java

public static boolean register(Context context) {
    LoggingUtils.i(LOG_TAG, "Registering default exceptions handler");
    // Get information about the Package
    PackageManager pm = context.getPackageManager();
    try {/*from ww w.ja  v a 2 s. c  o  m*/
        PackageInfo pi;
        // Version
        pi = pm.getPackageInfo(context.getPackageName(), 0);
        RA.APP_VERSION = pi.versionName;
        // Package name
        RA.APP_PACKAGE = pi.packageName;
        // Files dir for storing the stack traces
        RA.FILES_PATH = context.getFilesDir().getAbsolutePath();
        // Device model
        RA.PHONE_MODEL = android.os.Build.MODEL;
        // Android version
        RA.ANDROID_VERSION = android.os.Build.VERSION.RELEASE;
    } catch (NameNotFoundException e) {
        LoggingUtils.e(LOG_TAG,
                "No package found for \"" + context.getPackageName() + "\"" + e.getLocalizedMessage(), context);
    }

    LoggingUtils.i(LOG_TAG, "TRACE_VERSION: " + RA.TraceVersion);
    LoggingUtils.d(LOG_TAG, "APP_VERSION: " + RA.APP_VERSION);
    LoggingUtils.d(LOG_TAG, "APP_PACKAGE: " + RA.APP_PACKAGE);
    LoggingUtils.d(LOG_TAG, "FILES_PATH: " + RA.FILES_PATH);
    LoggingUtils.d(LOG_TAG, "URL: " + RA.URL);

    boolean stackTracesFound = false;
    // We'll return true if any stack traces were found
    if (searchForStackTraces().length > 0) {
        stackTracesFound = true;
    }

    new Thread() {
        @Override
        public void run() {
            // First of all transmit any stack traces that may be lying around
            submitStackTraces();
            UncaughtExceptionHandler currentHandler = Thread.getDefaultUncaughtExceptionHandler();
            if (currentHandler != null) {
                LoggingUtils.d(LOG_TAG, "current handler class=" + currentHandler.getClass().getName());
            }
            // don't register again if already registered
            if (!(currentHandler instanceof DefaultExceptionHandler)) {
                // Register default exceptions handler
                Thread.setDefaultUncaughtExceptionHandler(new DefaultExceptionHandler(currentHandler));
            }
        }
    }.start();

    return stackTracesFound;
}

From source file:com.westlinkin.android_feedback_helper.utils.MailUntils.java

public static String getMailBody(Context context, String msg, String userEmail) {
    String html = "";
    try {/* ww  w  .j a  v  a 2s.  c  o m*/
        html = IOUtils.toString(context.getAssets().open("android_feedback_helper.html"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (html.isEmpty())
        return null;

    // version name and code
    PackageManager packageManager = context.getPackageManager();
    String versionName = "Unknown";
    int versionCode = -1;
    if (packageManager != null) {
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            versionName = packageInfo.versionName;
            versionCode = packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    Date now = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd_HH.mm.ss_zzz");
    String time = sdf.format(now);

    return html.replaceAll(Data.APP_PACKAGE, context.getPackageName())
            .replaceAll(Data.VERSION_NAME, versionName)
            .replaceAll(Data.VERSION_CODE, String.valueOf(versionCode)).replaceAll(Data.FEEDBACK_TIME, time)
            .replaceAll(Data.FEEDBACK_MESSAGE, msg).replaceAll(Data.USER_EMAIL, userEmail);
}