Java Code Examples for android.content.pm.PackageManager#getPackageInfo()

The following examples show how to use android.content.pm.PackageManager#getPackageInfo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From SecuritySample with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the encoded representation of the first signing certificated used to sign current APK
 *
 * @param ctx
 * @return
 */
private static byte[] getSigningKeyCertificate(Context ctx) {
    try {
        PackageManager pm = ctx.getPackageManager();
        String packageName = ctx.getPackageName();
        int flags = PackageManager.GET_SIGNATURES;
        PackageInfo packageInfo = pm.getPackageInfo(packageName, flags);
        Signature[] signatures = packageInfo.signatures;

        if (signatures != null && signatures.length >= 1) {
            //takes just the first signature, TODO: handle multi signed apks
            byte[] cert = signatures[0].toByteArray();
            InputStream input = new ByteArrayInputStream(cert);
            CertificateFactory cf = CertificateFactory.getInstance("X509");
            X509Certificate c = (X509Certificate) cf.generateCertificate(input);
            return c.getEncoded();
        }
    } catch (Exception e) {
        Log.w(TAG, e);
    }
    return null;
}
 
Example 2
Source File: BasicUtils.java    From UltimateAndroid with Apache License 2.0 6 votes vote down vote up
/**
 * get the version name which defines in AndroidManifest.xml
 *
 * @param context
 * @return
 */
public static String getVersionName(Context context) {
    String version = "";
    try {
        // get packagemanager
        PackageManager packageManager = context.getPackageManager();
        // getPackageName()--your current package name,0 means get package info
        PackageInfo packInfo = packageManager.getPackageInfo(context
                .getPackageName(), 0);
        version = packInfo.versionName;

    } catch (Exception e) {
        e.printStackTrace();
        Logs.e(e.getMessage());

    } finally {
        return version;
    }

}
 
Example 3
Source File: SettingsFragment.java    From ResearchStack with Apache License 2.0 6 votes vote down vote up
public String getVersionString() {
    int versionCode;
    String versionName;
    PackageManager manager = getActivity().getPackageManager();

    try {
        PackageInfo info = manager.getPackageInfo(getActivity().getPackageName(), 0);
        versionCode = info.versionCode;
        versionName = info.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        LogExt.e(getClass(), "Could not find package version info");
        versionCode = 0;
        versionName = getString(R.string.rss_settings_version_unknown);
    }
    return getString(R.string.rss_settings_version, versionName, versionCode);
}
 
Example 4
Source File: AppInfo.java    From TurboLauncher with Apache License 2.0 6 votes vote down vote up
/**
 * Must not hold the Context.
 */
public AppInfo(PackageManager pm, ResolveInfo info, IconCache iconCache,
		HashMap<Object, CharSequence> labelCache) {
	final String packageName = info.activityInfo.applicationInfo.packageName;

	this.componentName = new ComponentName(packageName,
			info.activityInfo.name);
	this.container = ItemInfo.NO_ID;
	this.setActivity(componentName, Intent.FLAG_ACTIVITY_NEW_TASK
			| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

	try {
		PackageInfo pi = pm.getPackageInfo(packageName, 0);
		flags = initFlags(pi);
		firstInstallTime = initFirstInstallTime(pi);
	} catch (NameNotFoundException e) {

	}

	iconCache.getTitleAndIcon(this, info, labelCache);
}
 
Example 5
Source File: CrashHandler.java    From StarBarcode with Apache License 2.0 5 votes vote down vote up
/**
 * 获取手机各项信息
 * @param pw
 */
private void dumpPhoneInfo(PrintWriter pw) throws PackageManager.NameNotFoundException {
    //得到包管理器
    PackageManager pm = mContext.getPackageManager();
    //得到包对象
    PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(),PackageManager.GET_ACTIVITIES);
    //写入APP版本号
    pw.print("StarBarcodeApp Version: ");
    pw.print(pi.versionName);
    pw.print("_");
    pw.println(pi.versionCode);
    //写入 Android 版本号
    pw.print("OS Version: ");
    pw.print(Build.VERSION.RELEASE);
    pw.print("_");
    pw.println(Build.VERSION.SDK_INT);
    //手机制造商
    pw.print("Vendor: ");
    pw.println(Build.MANUFACTURER);
    //手机型号
    pw.print("Model: ");
    pw.println(Build.MODEL);
    //CPU架构
    pw.print("CPU ABI: ");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        pw.println(Build.SUPPORTED_ABIS);
    }else {
        pw.println(Build.CPU_ABI);
    }
}
 
Example 6
Source File: ControlFactory.java    From AndroidStudyDemo with GNU General Public License v2.0 5 votes vote down vote up
private static int getVersionCode(Context context) {
    try {
        PackageManager packageManager = context.getPackageManager();
        PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
        return packInfo.versionCode;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return 0;
}
 
Example 7
Source File: PackageManagerUtils.java    From brailleback with Apache License 2.0 5 votes vote down vote up
private static PackageInfo getPackageInfo(Context context, CharSequence packageName) {
    if (packageName == null) {
        return null;
    }

    final PackageManager packageManager = context.getPackageManager();

    try {
        return packageManager.getPackageInfo(packageName.toString(), 0);
    } catch (NameNotFoundException e) {
        return null;
    }
}
 
Example 8
Source File: PackageUtils.java    From SmsCode with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 指定的包名对应的App是否已安装
 *
 */
public static boolean isPackageInstalled(Context context, String packageName) {
    PackageManager pm = context.getPackageManager();
    try {
        PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
        return packageInfo != null;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return false;
}
 
Example 9
Source File: CrashManagerConstants.java    From iBeebo with GNU General Public License v3.0 5 votes vote down vote up
public static void loadFromContext(Context context) {

        CrashManagerConstants.ANDROID_VERSION = android.os.Build.VERSION.RELEASE;
        CrashManagerConstants.PHONE_MODEL = android.os.Build.MODEL;
        CrashManagerConstants.PHONE_MANUFACTURER = android.os.Build.MANUFACTURER;

        PackageManager packageManager = context.getPackageManager();
        try {
            PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            CrashManagerConstants.APP_VERSION = "" + packageInfo.versionCode;
            CrashManagerConstants.APP_PACKAGE = packageInfo.packageName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }
 
Example 10
Source File: SystemSettingFragment.java    From VIA-AI with MIT License 5 votes vote down vote up
private String getVersionName(Context context){
    PackageManager packageManager = context.getPackageManager();
    PackageInfo packageInfo;
    String versionName = "";
    try {
        packageInfo = packageManager.getPackageInfo(context.getPackageName(),0);
        versionName = packageInfo.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return versionName;
}
 
Example 11
Source File: Kits.java    From XDroidMvp with MIT License 5 votes vote down vote up
/**
 * 获取当前版本
 *
 * @param context
 * @return
 */
public static String getVersionName(Context context) {
    PackageManager pManager = context.getPackageManager();
    PackageInfo packageInfo = null;
    try {
        packageInfo = pManager.getPackageInfo(context.getPackageName(), 0);
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return packageInfo.versionName;
}
 
Example 12
Source File: DisallowedProviders.java    From SafeContentResolver with Apache License 2.0 5 votes vote down vote up
private ProviderInfo[] getProviderInfo(Context context) {
    try {
        PackageManager packageManager = context.getPackageManager();
        String packageName = context.getPackageName();
        PackageInfo packageInfo = packageManager.getPackageInfo(packageName,
                PackageManager.GET_PROVIDERS | PackageManager.GET_META_DATA);

        ProviderInfo[] providers = packageInfo.providers;
        return providers != null ? providers : new ProviderInfo[0];
    } catch (NameNotFoundException e) {
        throw new RuntimeException(e);
    }
}
 
Example 13
Source File: VersionHelper.java    From ESeal with Apache License 2.0 5 votes vote down vote up
/**
 * 获取版本号
 *
 * @param context
 * @return
 */
public static String getVersionCode(Context context) {
    PackageManager packageManager = context.getPackageManager();
    PackageInfo packageInfo;
    String versionCode = "";
    try {
        packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
        versionCode = packageInfo.versionCode + "";
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
    return versionCode;
}
 
Example 14
Source File: ChromeDiscoveryHandler.java    From stetho with MIT License 5 votes vote down vote up
private String getAppLabelAndVersion() {
  StringBuilder b = new StringBuilder();
  PackageManager pm = mContext.getPackageManager();
  b.append(getAppLabel());
  b.append('/');
  try {
    PackageInfo info = pm.getPackageInfo(mContext.getPackageName(), 0 /* flags */);
    b.append(info.versionName);
  } catch (PackageManager.NameNotFoundException e) {
    throw new RuntimeException(e);
  }
  return b.toString();
}
 
Example 15
Source File: HelpActivity.java    From GcmForMojo with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 获取版本号
 * @return 当前应用的版本号
 */
private String getVersion() {
    try {
        PackageManager manager = this.getPackageManager();
        PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
        return "("+info.versionName+" By jklmn of SWJTU)";
    } catch (Exception e) {
        e.printStackTrace();
        return this.getString(R.string.can_not_find_version_name);
    }
}
 
Example 16
Source File: Tools.java    From dtube-mobile-unofficial with Apache License 2.0 5 votes vote down vote up
static boolean isPackageInstalled(String packageName, PackageManager packageManager) {
    boolean found = true;
    try {
        packageManager.getPackageInfo(packageName, 0);
    } catch (PackageManager.NameNotFoundException e) {
        found = false;
    }
    return found;
}
 
Example 17
Source File: Util.java    From VideoCompressor with Apache License 2.0 5 votes vote down vote up
public static String getVersionName(Context context){
    PackageManager packageManager = context.getPackageManager();
    PackageInfo packInfo = null;
    String version = null;
    try {
        packInfo = packageManager.getPackageInfo(context.getPackageName(),0);
        version = packInfo.versionName;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }

    return version;
}
 
Example 18
Source File: CommonUtils.java    From ans-android-sdk with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 获取应用版本名称
 */
public static String getVersionName(Context context) {
    try {
        final PackageManager packageManager = context.getPackageManager();
        final PackageInfo packageInfo = packageManager.
                getPackageInfo(context.getPackageName(), 0);
        return packageInfo.versionName;
    } catch (Throwable ignore) {
        ExceptionUtil.exceptionThrow(ignore);
        return Constants.EMPTY;
    }
}
 
Example 19
Source File: SensorSettingsActivity.java    From Sensor-Disabler with MIT License 5 votes vote down vote up
private static boolean isPackageInstalled(String packagename, Context context) {
    PackageManager pm = context.getPackageManager();
    try {
        pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}
 
Example 20
Source File: Installed.java    From Saiy-PS with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * Get a list of all of the installed applications that hold the {@link Manifest.permission#CONTROL_SAIY},
 * regardless of whether or not the permission has been explicitly granted. The results exclude our own
 * package.
 * <p>
 * This method is slower than {@link #declaresSaiyPermissionLegacy(Context)} but avoids the 'transaction
 * too large' errors.
 *
 * @param ctx the application context
 * @return an ArrayList containing a {@link Pair} with the first parameter containing the application name
 * and the second the package name.
 */
public static ArrayList<Pair<String, String>> declaresSaiyPermission(@NonNull final Context ctx) {

    final long then = System.nanoTime();

    final ArrayList<Pair<String, String>> holdsPermission = new ArrayList<>();
    final PackageManager pm = ctx.getPackageManager();
    final List<ApplicationInfo> apps = pm.getInstalledApplications(0);

    CharSequence appNameChar;
    String appName;
    String[] permissions;
    PackageInfo packageInfo;
    for (final ApplicationInfo applicationInfo : apps) {

        try {

            packageInfo = pm.getPackageInfo(applicationInfo.packageName, PackageManager.GET_PERMISSIONS);

            permissions = packageInfo.requestedPermissions;
            if (permissions != null) {

                for (final String permission : permissions) {
                    if (pCONTROL_SAIY.matcher(permission).matches()) {

                        appNameChar = packageInfo.applicationInfo.loadLabel(pm);

                        if (appNameChar != null) {
                            appName = packageInfo.applicationInfo.loadLabel(pm).toString();

                            if (UtilsString.notNaked(appName)) {
                                if (!pSAIY_PACKAGE.matcher(packageInfo.packageName).matches()) {
                                    holdsPermission.add(new Pair<>(appName, packageInfo.packageName));
                                }
                                break;
                            }
                        }

                        holdsPermission.add(new Pair<>(packageInfo.packageName, packageInfo.packageName));
                    }
                }
            }
        } catch (final PackageManager.NameNotFoundException e) {
            if (DEBUG) {
                MyLog.w(CLS_NAME, "isPackageInstalled: NameNotFoundException");
            }
        }
    }

    if (DEBUG) {
        MyLog.getElapsed(Installed.class.getSimpleName(), then);
    }

    return holdsPermission;
}