管理一些手机中的Apk或者其他应用程序:

一、自己封装的类ApkManager,关于Apk的操作,很少,日后再行补充。

(1) 获取当前程序版本   



 public static String getVersionName(Context context) {

		PackageManager packageManager = null;

		PackageInfo packInfo = null;

		try {

			packageManager = context.getPackageManager();

// getPackageName()获取当前程序包名, 0代表获取版本号

			packInfo = packageManager.getPackageInfo(context.getPackageName(),

					0);

			return packInfo.versionName;

		} catch (NameNotFoundException e) {

			e.printStackTrace();

		}

		return null;

	}

(2) 获取当前程序版本号   

public static int getVersionCode(Context context) {

		PackageManager packageManager = null;

		PackageInfo packInfo = null;

		try {

			packageManager = context.getPackageManager();

// getPackageName()获取当前程序包名, 0代表获取版本号

			packInfo = packageManager.getPackageInfo(context.getPackageName(),

					0);

			return packInfo.versionCode;

		} catch (NameNotFoundException e) {

			e.printStackTrace();

		}

		return 0;

	}

(3)安装Apk     

public static void installApk(Context context, File apk) {

		Intent in = new Intent();

		in.setAction(Intent.ACTION_VIEW);

		in.setDataAndType(Uri.fromFile(apk),

				"application/vnd.android.package-archive");

		context.startActivity(in);

	}

 

二、封装类InstallManager,关于安装包的一些操作。

(1)获取本地安装所有的非系统应用   

public static List<AppInfoBean> getAllInstalledPackages(Context context) {

		List<AppInfoBean> app_list = new ArrayList<AppInfoBean>();

// 获取所有的安装包

		PackageManager pManager = context.getPackageManager();

		List<PackageInfo> packages = pManager.getInstalledPackages(0);



		for (PackageInfo pInfo : packages) {

            byte[] appIcon = BitmapManager.getBytesFromDrawable(pInfo.applicationInfo.loadIcon(pManager));

			AppInfoBean aib = new AppInfoBean(pInfo.applicationInfo.loadLabel(

					pManager).toString(), pInfo.packageName, pInfo.versionName,

					pInfo.versionCode, appIcon);



			if ((pInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {

// 这是系统应用程序

			} else {

				app_list.add(aib);

			}

		}

		return app_list;

	}

(2)根据包名判断App是否安装       

public static boolean isAppInstalled(Context context, String packageName) {

		PackageInfo packageInfo;

		try {

			packageInfo = context.getPackageManager().getPackageInfo(

					packageName, 0);

			if (packageInfo != null) {

				return true;

			}

		} catch (NameNotFoundException e) {

			packageInfo = null;

			Toast.makeText(context, "没有找到该应用,请检查是否正确安装", Toast.LENGTH_LONG)

					.show();

			e.printStackTrace();

		}

		return false;

	}

(3)启动App     

public static void startInstalledApp(Context context, String packageName) {

		if (packageName == null || packageName.equals("")) {

			Toast.makeText(context, "无效应用", Toast.LENGTH_LONG).show();

			return;

		}

// 检测是否安装,若成功安装,那么启动该App

		if (isAppInstalled(context, packageName)) {

			try {

				Intent in = context.getPackageManager()

						.getLaunchIntentForPackage(packageName);

				context.startActivity(in);

			} catch (Exception e) {

				Toast.makeText(context, "启动失败", Toast.LENGTH_LONG).show();

				e.printStackTrace();

			}

		}

	}

(4)获取App应用名       

public static String getAppName(Context context, String packageName){

		PackageManager pManager = null;

		ApplicationInfo mAppInfo = null;

		try {

			pManager = context.getPackageManager();

			mAppInfo = pManager.getApplicationInfo(packageName, 0);

		} catch (NameNotFoundException e) {

			e.printStackTrace();

			return null;

		}

		return mAppInfo.loadLabel(pManager).toString();

	}

(5)卸载App       

public static void uninstallApp(Context context, String packageName) {

		if (packageName == null || packageName.equals("")) {

			Toast.makeText(context, "无效的应用程序", Toast.LENGTH_LONG).show();

			return;

		}

         String appName = getAppName(context, packageName);

		if(appName == null || appName.equals("")){

			Toast.makeText(context, "您要卸载的程序不存在", Toast.LENGTH_LONG).show();

			return;

		}

		Uri packageUri = Uri.parse("package:" + packageName+appName);

		Intent in = new Intent(Intent.ACTION_DELETE, packageUri);

		context.startActivity(in);

	}

部分APP操作代码。