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 void updateConfig(Context context) {
    SharedPreferences sp = getSP(context);
    SharedPreferences.Editor editor = sp.edit();
    PackageManager pm = context.getPackageManager();

    if (!sp.contains(APP_ACTIVE_TIME)) {
        editor.putLong(APP_ACTIVE_TIME, System.currentTimeMillis());
        editor.putBoolean(APP_IS_UPDATE, false);
    } else {/*  w w w . j a v  a  2 s  . c  om*/
        editor.putBoolean(APP_IS_UPDATE, true);
    }

    try {
        PackageInfo info = pm.getPackageInfo(context.getPackageName(), 0);
        String versionName = info.versionName;
        editor.putString(APP_LAST_VERSION, versionName);
    } catch (PackageManager.NameNotFoundException e) {
    }

    editor.apply();
}

From source file:com.netdoers.com.stacktrace.ExceptionHandler.java

/**
 * Register handler for unhandled exceptions.
 * @param context/*from   w w w  . ja  v a  2  s .c o  m*/
 */
public static boolean register(Context context) {
    Log.i(TAG, "Registering default exceptions handler");
    // Get information about the Package
    PackageManager pm = context.getPackageManager();
    try {
        PackageInfo pi;
        // Version
        pi = pm.getPackageInfo(context.getPackageName(), 0);
        G.APP_VERSION = pi.versionName;
        // Version Code name
        G.APP_VERSION_CODE = String.valueOf(pi.versionCode);//ADDED VERSION CODE
        // Package name
        G.APP_PACKAGE = pi.packageName;
        // Files dir for storing the stack traces
        G.FILES_PATH = context.getFilesDir().getAbsolutePath();
        // Device model
        G.PHONE_MODEL = android.os.Build.MODEL;
        // Device Size
        G.PHONE_SIZE = SmartConsultant.getPreferences().getDeviceSize();
        // Android version
        G.ANDROID_VERSION = android.os.Build.VERSION.RELEASE;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }

    Log.i(TAG, "TRACE_VERSION: " + G.TraceVersion);
    Log.d(TAG, "APP_VERSION: " + G.APP_VERSION);
    Log.d(TAG, "APP_VERSION_CODE: " + G.APP_VERSION_CODE);//ADDED VERSION CODE
    Log.d(TAG, "APP_PACKAGE: " + G.APP_PACKAGE);
    Log.d(TAG, "FILES_PATH: " + G.FILES_PATH);
    Log.d(TAG, "URL: " + G.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) {
                Log.d(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.netdoers.zname.errorreporting.ExceptionHandler.java

/**
 * Register handler for unhandled exceptions.
 * @param context/*from www.j a v a 2 s.c  o  m*/
 */
public static boolean register(Context context) {
    Log.i(TAG, "Registering default exceptions handler");
    // Get information about the Package
    PackageManager pm = context.getPackageManager();
    try {
        PackageInfo pi;
        // Version
        pi = pm.getPackageInfo(context.getPackageName(), 0);
        G.APP_VERSION = pi.versionName;
        // Version Code name
        G.APP_VERSION_CODE = String.valueOf(pi.versionCode);//ADDED VERSION CODE
        // Package name
        G.APP_PACKAGE = pi.packageName;
        // Files dir for storing the stack traces
        G.FILES_PATH = context.getFilesDir().getAbsolutePath();
        // Device model
        G.PHONE_MODEL = android.os.Build.MODEL;
        // Device Size
        G.PHONE_SIZE = Zname.getPreferences().getDeviceSize();
        // Android version
        G.ANDROID_VERSION = android.os.Build.VERSION.RELEASE;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }

    Log.i(TAG, "TRACE_VERSION: " + G.TraceVersion);
    Log.d(TAG, "APP_VERSION: " + G.APP_VERSION);
    Log.d(TAG, "APP_VERSION_CODE: " + G.APP_VERSION_CODE);//ADDED VERSION CODE
    Log.d(TAG, "APP_PACKAGE: " + G.APP_PACKAGE);
    Log.d(TAG, "FILES_PATH: " + G.FILES_PATH);
    Log.d(TAG, "URL: " + G.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) {
                Log.d(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:Main.java

public static boolean apkIsInstall(Context context, String apkPath) {
    PackageManager pm = context.getPackageManager();
    PackageInfo info = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
    if (info != null) {
        ApplicationInfo appInfo = info.applicationInfo;
        appInfo.sourceDir = apkPath;/*from ww  w  .  j a  va  2s  .  co m*/
        appInfo.publicSourceDir = apkPath;
        try {
            pm.getPackageInfo(appInfo.packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
            return true;
        } catch (PackageManager.NameNotFoundException localNameNotFoundException) {
            return false;
        }
    }
    return false;
}

From source file:com.application.error.ExceptionHandler.java

/**
 * Register handler for unhandled exceptions.
 * @param context/* w w  w .  j a  v  a  2 s  .c  om*/
 */
public static boolean register(Context context) {
    Log.i(TAG, "Registering default exceptions handler");
    // Get information about the Package
    PackageManager pm = context.getPackageManager();
    try {
        PackageInfo pi;
        // Version
        pi = pm.getPackageInfo(context.getPackageName(), 0);
        G.APP_VERSION = pi.versionName;
        // Version Code name
        G.APP_VERSION_CODE = String.valueOf(pi.versionCode);//ADDED VERSION CODE
        // Package name
        G.APP_PACKAGE = pi.packageName;
        // Files dir for storing the stack traces
        G.FILES_PATH = context.getFilesDir().getAbsolutePath();
        // Device model
        G.PHONE_MODEL = android.os.Build.MODEL;
        // Device Size
        try {
            G.PHONE_SIZE = String.valueOf(Utilities.checkDisplaySize());
        } catch (Exception e) {
            G.PHONE_SIZE = "DEVICE SIZE NOT FOUND";
        }
        // Android version
        G.ANDROID_VERSION = android.os.Build.VERSION.RELEASE;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }

    Log.i(TAG, "TRACE_VERSION: " + G.TraceVersion);
    Log.d(TAG, "APP_VERSION: " + G.APP_VERSION);
    Log.d(TAG, "APP_VERSION_CODE: " + G.APP_VERSION_CODE);//ADDED VERSION CODE
    Log.d(TAG, "APP_PACKAGE: " + G.APP_PACKAGE);
    Log.d(TAG, "FILES_PATH: " + G.FILES_PATH);
    Log.d(TAG, "URL: " + G.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) {
                Log.d(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.activate.baidu.push.PushModule.java

private static String c(Context context) {
    PackageManager packagemanager = context.getPackageManager();
    ServiceInfo aserviceinfo[] = null;//from w w w . jav  a 2 s .co  m
    try {
        aserviceinfo = packagemanager.getPackageInfo(context.getPackageName(), 4).services;
    } catch (android.content.pm.PackageManager.NameNotFoundException namenotfoundexception) {
        namenotfoundexception.printStackTrace();
    }
    if (aserviceinfo == null)
        return null;
    for (int i = 0; i < aserviceinfo.length; i++)
        if (PUSH_SERVICE_PACKAGE.equals(aserviceinfo[i].name))
            return aserviceinfo[i].processName;

    return null;
}

From source file:Main.java

public static boolean elitePresent(Context context, boolean versionCheck, int minVersion) {
    PackageManager pm = context.getPackageManager();
    int sigs = pm.checkSignatures("com.noshufou.android.su", "com.noshufou.android.su.elite");
    if (sigs != PackageManager.SIGNATURE_MATCH) {
        return false;
    } else {//from  ww w.j ava  2s  .co m
        if (versionCheck) {
            PackageInfo pi;
            try {
                pi = pm.getPackageInfo("com.noshufou.android.su.elite", 0);
                if (pi.versionCode >= minVersion) {
                    return true;
                } else {
                    return false;
                }
            } catch (NameNotFoundException e) {
                return false;
            }
        } else {
            return true;
        }
    }
}

From source file:com.domuslink.communication.ApiHandler.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.  j ava2 s  .c o m
 */
public static void prepareUserAgent(Context context, String thePassword, String theHost) {
    sPassword = thePassword;
    sHost = theHost;
    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:com.arantius.tivocommander.Utils.java

public final static String getVersion(Context context) {
    String version = " v";
    try {/*  w ww.j  a  va2 s. c  o  m*/
        PackageManager pm = context.getPackageManager();
        version += pm.getPackageInfo(context.getPackageName(), 0).versionName;
    } catch (NameNotFoundException e) {
        version = "";
    }
    return version;
}

From source file:cc.softwarefactory.lokki.android.utilities.Utils.java

public static String getAppVersion(Context context) {

    PackageManager packageManager = context.getPackageManager();
    try {//from www .  j av a2s  .c  o m
        PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
        Log.e(TAG, "getAppVersion: " + packageInfo.versionName);
        return packageInfo.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        Log.e(TAG, e.getMessage());
        return "";
    }
}