private String getVersion() { // get the PackageManager PackageManager manager = this.getPackageManager(); try { PackageInfo info = manager.getPackageInfo(getPackageName(), 0); return info.versionName; } catch (NameNotFoundException e) { e.printStackTrace(); return this.getString(R.string.text_version); } }
说明:在Activity中首先可以调用this.getPackageManager()方法得到PackageManager对象,通过manager对象可以通过调用getPackageInfo(packageName,0);方法,得到PackInfo对象,然后就可以取得应用程序的“版本.........”等相关信息。
2.手机中安装的应用程序的相关信息
private static final String TAG = "AppManagerProvider"; private Context context; private PackageManager manager; public AppManagerProvider(Context context) { this.context = context; manager = context.getPackageManager(); } /** * return the list of the phone application * @return */ public List<AppManagerInfo> getAppManagerInfo(){ List<AppManagerInfo> lists = new ArrayList<AppManagerInfo>(); List<PackageInfo> infos = manager.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES); for (PackageInfo packageInfo : infos) { AppManagerInfo myApp = new AppManagerInfo(); String packName = packageInfo.packageName; myApp.setPackName(packName); ApplicationInfo applicationInfo = packageInfo.applicationInfo; Drawable icon = applicationInfo.loadIcon(manager); myApp.setIcon(icon); String appName = (String) applicationInfo.loadLabel(manager); myApp.setAppName(appName); if(filterApp(applicationInfo)){ Log.i(TAG, "三方应用"); myApp.setSystemApp(false); }else{ Log.i(TAG, "系统应用"); myApp.setSystemApp(true); } lists.add(myApp); } return lists; } public boolean filterApp(ApplicationInfo info){ if((info.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0 ){ return true; }else if((info.flags & ApplicationInfo.FLAG_SYSTEM) == 0){ return true; } return false; }