下面列出了android.content.pm.PackageManager.NameNotFoundException#printStackTrace() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* get app version code
*
* @param context
* @return
*/
public static int getAppVersionCode(Context context) {
if (context != null) {
PackageManager pm = context.getPackageManager();
if (pm != null) {
PackageInfo pi;
try {
pi = pm.getPackageInfo(context.getPackageName(), 0);
if (pi != null) {
return pi.versionCode;
}
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
}
return -1;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
// Version
try {((TextView) findViewById(R.id.version_tv)).setText("" + getPackageManager().getPackageInfo(getPackageName(), 0).versionName);
} catch (NameNotFoundException e) {e.printStackTrace();}
// Backbutton
((Button)findViewById(R.id.back_button)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
static Pair<String, Resources> findSystemApk(String action, PackageManager pm) {
final Intent intent = new Intent(action);
for (ResolveInfo info : pm.queryBroadcastReceivers(intent, 0)) {
if (info.activityInfo != null &&
(info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
final String packageName = info.activityInfo.packageName;
try {
final Resources res = pm.getResourcesForApplication(packageName);
return Pair.create(packageName, res);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
}
return null;
}
private void loadAbout(){
PackageInfo pi = null;
try {
pi = getContext().getPackageManager().getPackageInfo(getContext().getPackageName(), 0);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
mAppNameText.setText("ColorPickerView");
mVersionText.setText("Version" + " " + (pi != null ? pi.versionName : "null"));
String s = "<b>Developed By:</b><br>Daniel Nilsson<br>";
mAboutText.setText(Html.fromHtml(s));
}
/**
* 获取指定程序信息
*/
public static ApplicationInfo getApplicationInfo(Context context, String pkg) {
try {
return context.getPackageManager().getApplicationInfo(pkg, 0);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取版本code
*
* @return
*/
public static int getVersionCode() {
PackageManager packageManager = instance.getPackageManager();
PackageInfo packageInfo = null;
try {
packageInfo = packageManager.getPackageInfo(instance.getPackageName(), 0);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return packageInfo.versionCode;
}
public void addCustomInfoToDataBase(Drawable icon, ItemInfo info, CharSequence title) {
LauncherActivityInfo app = mLauncherApps.resolveActivity(info.getIntent(), info.user);
final ComponentKey key = new ComponentKey(app.getComponentName(), app.getUser());
CacheEntry entry = mCache.get(key);
PackageInfo packageInfo = null;
try {
packageInfo = mPackageManager.getPackageInfo(
app.getComponentName().getPackageName(), 0);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
// We can't reuse the entry if the high-res icon is not present.
if (entry == null || entry.isLowResIcon || entry.icon == null) {
entry = new CacheEntry();
}
entry.icon = LauncherIcons.createIconBitmap(icon, mContext);
entry.title = title != null ? title : app.getLabel();
entry.contentDescription = mUserManager.getBadgedLabelForUser(entry.title, app.getUser());
mCache.put(key, entry);
Bitmap lowResIcon = generateLowResIcon(entry.icon, mActivityBgColor);
ContentValues values = newContentValues(entry.icon, lowResIcon, entry.title.toString(),
app.getApplicationInfo().packageName);
if (packageInfo != null) {
addIconToDB(values, app.getComponentName(), packageInfo,
mUserManager.getSerialNumberForUser(app.getUser()));
}
}
@Override
public void onReceive(Context context, Intent intent) {
try {
DownloaderClientMarshaller.startDownloadServiceIfRequired(context, intent, SampleDownloaderService.class);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
public String getAPPVersion() {
if (mContext == null) {
return "";
}
try {
PackageInfo packInfo = mContext.getPackageManager().getPackageInfo(
mContext.getPackageName(), 0);
return packInfo.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return "";
}
@Override
public void onCreate() {
super.onCreate();
try {
mPackageInfo = getPackageManager().getPackageInfo(
getPackageName(), 0);
ApplicationInfo ai = getApplicationInfo();
CharSequence applicationLabel = getPackageManager().getApplicationLabel(ai);
mNotification = new DownloadNotification(this, applicationLabel);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
private String b() {
try {
PackageInfo packageInfo = Global.getContext().getPackageManager().getPackageInfo("com.tencent.android.qqdownloader", 0);
if (packageInfo == null) {
return null;
}
return packageInfo.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
return null;
}
}
/**
* 获取软件版本号名称
*
* @param context
* @return
*/
public static String getVersionCodeName(Context context) {
String versionCode = "";
try {
// 获取软件版本号,对应AndroidManifest.xml下android:versionCode
versionCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;
}
public static String v(Context context) {
PackageInfo packageInfo;
String str = null;
try {
packageInfo = context.getPackageManager().getPackageInfo(u(context), 64);
} catch (NameNotFoundException e) {
e.printStackTrace();
Object obj = str;
}
InputStream byteArrayInputStream = new ByteArrayInputStream(packageInfo.signatures[0].toByteArray());
try {
CertificateFactory instance = CertificateFactory.getInstance("X509");
} catch (CertificateException e2) {
e2.printStackTrace();
obj = str;
}
try {
X509Certificate x509Certificate = (X509Certificate) instance.generateCertificate(byteArrayInputStream);
} catch (CertificateException e22) {
e22.printStackTrace();
obj = str;
}
try {
str = a(MessageDigest.getInstance(CommonUtils.MD5_INSTANCE).digest(x509Certificate.getEncoded()));
} catch (NoSuchAlgorithmException e3) {
e3.printStackTrace();
} catch (CertificateEncodingException e4) {
e4.printStackTrace();
}
return str;
}
public static String getSign(Context context, String pkgName) {
try {
PackageInfo pis = context.getPackageManager().getPackageInfo(
pkgName, PackageManager.GET_SIGNATURES);
return MD5.hexdigest(pis.signatures[0].toByteArray());
} catch (NameNotFoundException e) {
e.printStackTrace();
return null;
}
}
public static String getAppVersionName(Context context) {
String versionName = "1.0";
try {
PackageInfo info = context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0);
versionName = info.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return versionName;
}
private boolean initIfNecessary() {
if ( !mInit ) {
Context ctx = getContext();
PackageManager pm = ctx.getPackageManager();
ProviderInfo pi = pm.resolveContentProvider(getAuthority(), PackageManager.GET_META_DATA);
PackageInfo packInfo;
try {
packInfo = pm.getPackageInfo(ctx.getPackageName(), 0);
} catch (NameNotFoundException e1) {
e1.printStackTrace();
return false;
}
int patchFileVersion;
int mainFileVersion;
int appVersionCode = packInfo.versionCode;
String[] resourceFiles = null;
if ( null != pi.metaData ) {
mainFileVersion = pi.metaData.getInt("mainVersion", appVersionCode);
patchFileVersion = pi.metaData.getInt("patchVersion", appVersionCode);
String mainFileName = pi.metaData.getString("mainFilename");
mainFileName = mainFileName != null? mainFileName : NO_FILE;
if ( NO_FILE != mainFileName ) {
String patchFileName = pi.metaData.getString("patchFilename");
patchFileName = patchFileName != null? patchFileName : NO_FILE;
if ( NO_FILE != patchFileName ) {
resourceFiles = new String[] { mainFileName, patchFileName };
} else {
resourceFiles = new String[] { mainFileName };
}
}
} else {
mainFileVersion = patchFileVersion = appVersionCode;
}
try {
if ( null == resourceFiles ) {
mAPKExtensionFile = APKExpansionSupport.getAPKExpansionZipFile(ctx, mainFileVersion, patchFileVersion);
} else {
mAPKExtensionFile = APKExpansionSupport.getResourceZipFile(resourceFiles);
}
mInit = true;
return true;
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
private boolean initIfNecessary() {
if ( !mInit ) {
Context ctx = getContext();
PackageManager pm = ctx.getPackageManager();
ProviderInfo pi = pm.resolveContentProvider(getAuthority(), PackageManager.GET_META_DATA);
PackageInfo packInfo;
try {
packInfo = pm.getPackageInfo(ctx.getPackageName(), 0);
} catch (NameNotFoundException e1) {
e1.printStackTrace();
return false;
}
int patchFileVersion;
int mainFileVersion;
int appVersionCode = packInfo.versionCode;
String[] resourceFiles = null;
if ( null != pi.metaData ) {
mainFileVersion = pi.metaData.getInt("mainVersion", appVersionCode);
patchFileVersion = pi.metaData.getInt("patchVersion", appVersionCode);
String mainFileName = pi.metaData.getString("mainFilename", NO_FILE);
if ( NO_FILE != mainFileName ) {
String patchFileName = pi.metaData.getString("patchFilename", NO_FILE);
if ( NO_FILE != patchFileName ) {
resourceFiles = new String[] { mainFileName, patchFileName };
} else {
resourceFiles = new String[] { mainFileName };
}
}
} else {
mainFileVersion = patchFileVersion = appVersionCode;
}
try {
if ( null == resourceFiles ) {
mAPKExtensionFile = APKExpansionSupport.getAPKExpansionZipFile(ctx, mainFileVersion, patchFileVersion);
} else {
mAPKExtensionFile = APKExpansionSupport.getResourceZipFile(resourceFiles);
}
mInit = true;
return true;
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
private boolean initIfNecessary() {
if (!mInit) {
Context ctx = getContext();
PackageManager pm = ctx.getPackageManager();
ProviderInfo pi = pm.resolveContentProvider(getAuthority(),
PackageManager.GET_META_DATA);
PackageInfo packInfo;
try {
packInfo = pm.getPackageInfo(ctx.getPackageName(), 0);
} catch (NameNotFoundException e1) {
e1.printStackTrace();
return false;
}
int patchFileVersion;
int mainFileVersion;
int appVersionCode = packInfo.versionCode;
String[] resourceFiles = null;
if (null != pi.metaData) {
mainFileVersion = pi.metaData.getInt("mainVersion", appVersionCode);
patchFileVersion = pi.metaData.getInt("patchVersion", appVersionCode);
String mainFileName = pi.metaData.getString("mainFilename");
if (null != mainFileName) {
String patchFileName = pi.metaData.getString("patchFilename");
if (null != patchFileName) {
resourceFiles = new String[] {
mainFileName, patchFileName
};
} else {
resourceFiles = new String[] {
mainFileName
};
}
}
} else {
mainFileVersion = patchFileVersion = appVersionCode;
}
try {
if (null == resourceFiles) {
mAPKExtensionFile = APKExpansionSupport.getAPKExpansionZipFile(ctx,
mainFileVersion, patchFileVersion);
} else {
mAPKExtensionFile = APKExpansionSupport.getResourceZipFile(resourceFiles);
}
mInit = true;
return true;
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
@Override
public void onCreate() {
Log.i(TAG, "onCreate");
connectivityManager = (ConnectivityManager) this
.getSystemService(Context.CONNECTIVITY_SERVICE);
telephonyManager = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
PackageManager packageManager = this.getPackageManager();
ApplicationInfo info = null;
try {
info = packageManager.getApplicationInfo(this.getPackageName(),
PackageManager.GET_META_DATA);
mUid = info.uid;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
timer = new Timer();
timerTask = new TimerTask() {
@Override
public void run() {
getTrafficStats();
}
};
if (timer != null && timerTask != null) {
preTx = curTx = TrafficStats.getUidTxBytes(mUid);
preRx = curRx = TrafficStats.getUidRxBytes(mUid);
Log.e(TAG, "cur" + curRx);
timer.schedule(timerTask, 0, SAMPLING_RATE);
// check the network operator is changed or not
operatorName = telephonyManager.getNetworkOperatorName();
if (!ConfigUtils.getString(this,
ConfigUtils.KEY_Network_Operator_Name).equals(operatorName)) {
ConfigUtils.setString(this,
ConfigUtils.KEY_Network_Operator_Name, operatorName);
ConfigUtils.setLong(this, ConfigUtils.KEY_RX_MOBILE, 0L);
ConfigUtils.setLong(this, ConfigUtils.KEY_TX_MOBILE, 0L);
// TODO Network operator has changed
}
}
}
/**
* 获取软件版本号
* <p/>
* It's weird, I don't know why.
* <pre>
* versionName -> versionCode
* 0.0.1 -> 12
* 0.3.4 -> 3042
* 3.2.4 -> 302042
* 12.234.221 -> 1436212
* </pre>
*
* @param context
* @return
*/
private int getVersionCodeLocal(Context context) {
LOG.d(TAG, "getVersionCode..");
int versionCode = 0;
try {
// 获取软件版本号,对应AndroidManifest.xml下android:versionCode
versionCode = context.getPackageManager().getPackageInfo(packageName, 0).versionCode;
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return versionCode;
}