getInstalledPackages、ResolveInfo 获取apk的信息

package com.example.packageinfo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;

public class MainActivity extends Activity {

	private static PackageManager pManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		/** 获取安装的非系统应用 */
		List<PackageInfo> packageInfos = getAllApps(this);
		for (int i = 0; i < packageInfos.size(); i++) {
			PackageInfo packageInfo = packageInfos.get(i);

			Drawable packageIcon = pManager
					.getApplicationIcon(packageInfo.applicationInfo);
			String packageLabel = pManager.getApplicationLabel(
					packageInfo.applicationInfo).toString();
			String packageName = packageInfo.applicationInfo.packageName;
			int versionCode = packageInfo.versionCode;
			String versionName = packageInfo.versionName;

			/**
			 * packageIcon:android.graphics.drawable.BitmapDrawable@3ed19685,
			 * packageLabel鲁大师, 
			 * packageNamecom.ludashi.benchmark, 
			 * versionCode85,
			 * versionName7.7.0.16.0401
			 */
			Log.i("TAG", "packageIcon:" + packageIcon + ",packageLabel"
					+ packageLabel + ",packageName" + packageName
					+ ",versionCode" + versionCode + ",versionName"
					+ versionName);

		}

		List<ResolveInfo> shareApps = getShareApps(this);
		for (int i = 0; i < shareApps.size(); i++) {

			String packageName = shareApps.get(i).activityInfo.packageName;
			// 得到图标
			Drawable icon = shareApps.get(i).loadIcon(pManager);
			// 得到应用名称
			String appName = shareApps.get(i).loadLabel(pManager).toString();

			/**
			 * packageName:com.ludashi.benchmark,
			 * iconandroid.graphics.drawable.BitmapDrawable@6a7d248,
			 * appName鲁大师
			 */
			Log.i("TAG", "packageName:" + packageName + ",icon" + icon
					+ ",appName" + appName);
		}
	}

	/**
	 * 获取手机安装的非系统应用
	 * 
	 * @param context
	 * @return
	 */
	public static List<PackageInfo> getAllApps(Context context) {
		List<PackageInfo> apps = new ArrayList<PackageInfo>();
		pManager = context.getPackageManager();
		// 获取手机内所有应用
		List<PackageInfo> paklist = pManager.getInstalledPackages(0);
		for (int i = 0; i < paklist.size(); i++) {
			PackageInfo pak = (PackageInfo) paklist.get(i);
			// 该值大于0时,表示获取的应用为系统预装的应用,反之则为手动安装的应用
			if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
				apps.add(pak);
			}
		}
		return apps;
	}

	/**
	 * 获取手机ResolveInfo列表
	 * 
	 * @param context
	 * @return
	 */

	public static List<ResolveInfo> getShareApps(Context context) {
		List<ResolveInfo> mApps = new ArrayList<ResolveInfo>();

		Intent intent = new Intent();
		intent.setAction(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_LAUNCHER);

		PackageManager pManager = context.getPackageManager();
		mApps = pManager.queryIntentActivities(intent,
				PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);

		return mApps;
	}

}

相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页