本文實例講述了Android實現整理PackageManager獲取所有安裝程序信息的方法。分享給大家供大家參考,具體如下:
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);//獲取安裝程序的包名for (int i = 0; i < packs.size(); i++) { PackageInfo p = packs.get(i);//某個包信息 //打印:版本好,版本名,包名.... Log.i("", "-------" + p.versionCode + "-------" + p.versionName + "--------" + p.packageName + "-------" + p.applicationInfo);}versionCode, versionName 的值來源AndroidManifest.xml文件
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.testapk" android:versionCode="2" // p.versionCode android:versionName="Version1" // p.versionName>
在代碼中獲取當前應用程序的versionCode,versionName
int versionCode = 0;try { versionCode = getPackageManager().getPackageInfo(this.getPackageName(), 0).versionCode;} catch (NameNotFoundException e) { e.printStackTrace();}代碼:
// 通過檢測包名,判斷APK是否安裝private boolean checkPackageExist(boolean getSysPackages) { boolean packageExist = false; int versionCode = 0; try { versionCode = getPackageManager().getPackageInfo(this.getPackageName(), 0).versionCode; } catch (NameNotFoundException e) { e.printStackTrace(); } Log.i("", "-------" + this.getPackageName() + "-------" + versionCode);//獲取當前包名 List<PackageInfo> packs = getPackageManager().getInstalledPackages(0); for (int i = 0; i < packs.size(); i++) { PackageInfo p = packs.get(i); Log.i("", "-------" + p.versionCode + "-------" + p.versionName + "--------" + p.packageName + "-------" + p.applicationInfo); if ((!getSysPackages) && (p.versionName == null)) { continue; } if (p.packageName.equalsIgnoreCase(PACKAGENAME)) { packageExist = true; break; } } return packageExist; } //安裝APK private void installApk() { if (checkFileExist(fileRoot + fileName)) { Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(android.content.Intent.ACTION_VIEW); String type = "application/vnd.android.package-archive"; intent.setDataAndType(Uri.parse("file://" + fileRoot + fileName), type); startActivity(intent); } else { downloadapk(); }}通過以上代碼,再加上一些網路下載的代碼,就可以做一個簡體的應用市場了。
希望本文所述對大家Android程序設計有所幫助。
新聞熱點
疑難解答
圖片精選