您的位置:首页 > 其它

整理PackageManager,获取所有安装程序信息

2012-03-08 09:37 387 查看
转:http://blog.csdn.net/hshm20517/article/details/6157416

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();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐