本文整理汇总了Java中android.content.pm.PackageManager.getPackageInfo方法的典型用法代码示例。如果您正苦于以下问题:Java PackageManager.getPackageInfo方法的具体用法?Java PackageManager.getPackageInfo怎么用?Java PackageManager.getPackageInfo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.content.pm.PackageManager
的用法示例。
在下文中一共展示了PackageManager.getPackageInfo方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getAppVersion
import android.content.pm.PackageManager; //导入方法依赖的package包/类
/**
* manifest 中的 versionName 字段
*/
public static String getAppVersion(Context context) {
if (appVersionName == null) {
PackageManager manager = context.getPackageManager();
try {
android.content.pm.PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
appVersionName = info.versionName;
} catch (Exception e) {
e.printStackTrace();
}
}
if (appVersionName == null) {
return "";
} else {
return appVersionName;
}
}
示例2: getAppVersion
import android.content.pm.PackageManager; //导入方法依赖的package包/类
/**
* 返回版本信息
*
* @param context
* @return
*/
public static String getAppVersion(Context context)
{
PackageManager localPackageManager = context.getPackageManager();
try
{
String str = localPackageManager.getPackageInfo(context.getPackageName(), 0).versionName;
return str;
}
catch (PackageManager.NameNotFoundException e)
{
Log.E(TAG, "getAppVersion error" + e.getMessage());
e.printStackTrace();
}
return "";
}
示例3: getFingerprint
import android.content.pm.PackageManager; //导入方法依赖的package包/类
/**
* Gets the fingerprint of the signed certificate of a package.
*/
public static String getFingerprint(Context context, String packageName) throws Exception {
PackageManager pm = context.getPackageManager();
PackageInfo packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
Signature[] signatures = packageInfo.signatures;
if (signatures.length != 1) {
throw new SecurityException(packageName + " has " + signatures.length + " signatures");
}
byte[] cert = signatures[0].toByteArray();
try (InputStream input = new ByteArrayInputStream(cert)) {
CertificateFactory factory = CertificateFactory.getInstance("X509");
X509Certificate x509 = (X509Certificate) factory.generateCertificate(input);
MessageDigest md = MessageDigest.getInstance("SHA256");
byte[] publicKey = md.digest(x509.getEncoded());
return toHexFormat(publicKey);
}
}
示例4: onCreate
import android.content.pm.PackageManager; //导入方法依赖的package包/类
public void onCreate(Context context) {
AttributeCache.init(context);
PackageManager pm = context.getPackageManager();
PackageInfo packageInfo = null;
try {
packageInfo = pm.getPackageInfo(context.getPackageName(),
PackageManager.GET_ACTIVITIES | PackageManager.GET_PROVIDERS | PackageManager.GET_META_DATA);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (packageInfo == null) {
throw new RuntimeException("Unable to found PackageInfo : " + context.getPackageName());
}
sService.set(this);
}
示例5: dumpPhoneInfo
import android.content.pm.PackageManager; //导入方法依赖的package包/类
@SuppressWarnings("deprecation")
private void dumpPhoneInfo(PrintWriter pw) throws PackageManager.NameNotFoundException {
PackageManager pm = mContext.getPackageManager();
PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(),
PackageManager.GET_ACTIVITIES);
pw.print("App 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(SDK_INT);
/* 手机制造商 */
pw.print("Vendor: ");
pw.println(Build.MODEL);
/* CPU 架构 */
pw.print("CPU ABI: ");
pw.println(Build.CPU_ABI);
}
示例6: getAppVersionName
import android.content.pm.PackageManager; //导入方法依赖的package包/类
/**
* 返回app的版本名称.
*
* @param context the context
* @return app version name
*/
public static String getAppVersionName(Context context) {
String version = "unknown";
// 获取package manager的实例
PackageManager packageManager = context.getPackageManager();
// getPackageName()是你当前类的包名,0代表是获取版本信息
PackageInfo packInfo;
try {
packInfo = packageManager.getPackageInfo(context.getPackageName(),
0);
version = packInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
// Log.i("版本名称:", version);
return version;
}
示例7: getVersionCode
import android.content.pm.PackageManager; //导入方法依赖的package包/类
/**
* 获取应用的versionCode
*
* @return int类型的versionCode
*/
public static int getVersionCode(Context context) {
try {
PackageManager manager = context.getPackageManager();
PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
return info.versionCode;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
示例8: getAppInfo
import android.content.pm.PackageManager; //导入方法依赖的package包/类
/**
* 获取当前App信息
* <p>AppInfo(名称,图标,包名,版本号,版本Code,是否安装在SD卡,是否是用户程序)
*/
public static AppInfo getAppInfo(Context context) {
PackageManager pm = context.getPackageManager();
PackageInfo pi = null;
try {
pi = pm.getPackageInfo(context.getApplicationContext().getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return pi != null ? getBean(pm, pi) : null;
}
示例9: getAppPath
import android.content.pm.PackageManager; //导入方法依赖的package包/类
/**
* 获取App路径
*
* @param packageName 包名
* @return App路径
*/
public static String getAppPath(final String packageName) {
if (isSpace(packageName)) return null;
try {
PackageManager pm = Utils.getApp().getPackageManager();
PackageInfo pi = pm.getPackageInfo(packageName, 0);
return pi == null ? null : pi.applicationInfo.sourceDir;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return null;
}
}
示例10: getAppVersionCode
import android.content.pm.PackageManager; //导入方法依赖的package包/类
/**
* 获取版本号
*/
public static int getAppVersionCode(Context context) {
int versioncode = -1;
try {
// ---get the package info---
PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
versioncode = pi.versionCode;
} catch (Exception e) {
Log.e("VersionInfo", "Exception", e);
}
return versioncode;
}
示例11: check
import android.content.pm.PackageManager; //导入方法依赖的package包/类
/**
* Check package is support MiPush.
* @param pkg Package Name
* @param manager Package manager
* @return This package support status. If not use MiPush, will returns null
*/
@Nullable
public static SupportStatus check (String pkg,
PackageManager manager) {
try {
PackageInfo info = manager.getPackageInfo(pkg, PackageManager.GET_SERVICES |
PackageManager.GET_RECEIVERS | PackageManager.GET_DISABLED_COMPONENTS);
return check(info, manager);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return null;
}
}
示例12: getAppVersionName
import android.content.pm.PackageManager; //导入方法依赖的package包/类
/**
* 获取App版本号
*
* @param context 上下文
* @param packageName 包名
* @return App版本号
*/
public static String getAppVersionName(Context context, String packageName) {
if (StringCovertUtils.isSpace(packageName)) return null;
try {
PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageInfo(packageName, 0);
return pi == null ? null : pi.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return null;
}
}
示例13: getVersionCode
import android.content.pm.PackageManager; //导入方法依赖的package包/类
/**
* 根据包名获取程序的VersionCode
*
* @param pContext
* @param pPackageName
* @return
*/
public static final int getVersionCode(Context pContext, String pPackageName) {
PackageManager packageManager = pContext.getPackageManager();
PackageInfo packageInfo = null;
try {
packageInfo = packageManager.getPackageInfo(pPackageName, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
if (packageInfo != null) {
return packageInfo.versionCode;
} else {
return UNKNOWN_VERSION_CODE;
}
}
示例14: getApplicationIconDrawable
import android.content.pm.PackageManager; //导入方法依赖的package包/类
/**
* 获取应用启动图标
* @param context
* @return
* @throws Exception
*/
public static Drawable getApplicationIconDrawable(Context context) throws Exception {
PackageManager pm = context.getPackageManager();
String packageName = getTopActivityPackageName(context);
PackageInfo packageInfo = pm.getPackageInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
Drawable drawable = packageInfo.applicationInfo.loadIcon(pm);
return drawable;
}
示例15: getAppName
import android.content.pm.PackageManager; //导入方法依赖的package包/类
/**
* 获取App名称
*
* @param context 上下文
* @param packageName 包名
* @return App名称
*/
public static String getAppName(Context context, String packageName) {
if (DataUtil.isNullString(packageName))
return null;
try {
PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageInfo(packageName, 0);
return pi == null ? null : pi.applicationInfo.loadLabel(pm).toString();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return null;
}
}