好库网
package com.vanco.abplayer.view;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Environment;
import android.widget.Toast;

public class ApplicationUtils {
	/**
	 * 澶囦唤App 棣栧厛鏃犻渶鎻愬崌鏉冮檺灏卞氨鍙互澶嶅埗APK锛屾煡鐪嬫潈闄愪綘灏变細鐭ラ亾锛屽湪data/app涓嬬殑APK鏉冮檺濡備笅锛?rw-r--r-- system
	 * system 5122972 2012-12-13 10:38 com.taobao.taobao-1.apk 鎴戜滑鏄湁璇诲彇鏉冮檺鐨勩€?	 * 
	 * @param packageName
	 * @param mActivity
	 * @throws IOException
	 */
	public static void backupApp(String packageName, Activity mActivity)
			throws IOException {
		// 瀛樻斁浣嶇疆
		String newFile = Environment.getExternalStorageDirectory()
				.getAbsolutePath() + File.separator;
		String oldFile = null;
		try {
			// 鍘熷浣嶇疆
			oldFile = mActivity.getPackageManager().getApplicationInfo(
					packageName, 0).sourceDir;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		System.out.println(newFile);
		System.out.println(oldFile);

		File in = new File(oldFile);
		File out = new File(newFile + packageName + ".apk");
		if (!out.exists()) {
			out.createNewFile();
			Toast.makeText(mActivity, "鏂囦欢澶囦唤鎴愬姛锛? + "瀛樻斁浜? + newFile + "鐩綍涓?, 1)
					.show();
		} else {
			Toast.makeText(mActivity, "鏂囦欢宸茬粡瀛樺湪锛? + "鏌ョ湅" + newFile + "鐩綍涓?, 1)
					.show();
		}

		FileInputStream fis = new FileInputStream(in);
		FileOutputStream fos = new FileOutputStream(out);

		int count;
		// 鏂囦欢澶ぇ鐨勮瘽锛屾垜瑙夊緱闇€瑕佷慨鏀?		byte[] buffer = new byte[256 * 1024];
		while ((count = fis.read(buffer)) > 0) {
			fos.write(buffer, 0, count);
		}

		fis.close();
		fos.flush();
		fos.close();
	}

	/**
	 * 鑾峰彇褰撳墠Apk鐗堟湰鍙?android:versionCode
	 * 
	 * @param context
	 * @return
	 */
	public static int getVerCode(Context context) {
		int verCode = -1;
		try {
			verCode = context.getPackageManager().getPackageInfo(
					context.getPackageName(), 0).versionCode;
		} catch (NameNotFoundException e) {
		}
		return verCode;
	}

	public static String getVerName(Context context) {
		try {
			return context.getPackageManager().getPackageInfo(
					context.getPackageName(), 0).versionName;
		} catch (NameNotFoundException e) {
		}
		return "";
	}

	/**
	 * 杩斿洖褰撳墠鐨勫簲鐢ㄦ槸鍚﹀浜庡墠鍙版樉绀虹姸鎬?涓嶉渶瑕乤ndroid.permission.GET_TASKS鏉冮檺
	 * 链接已屏蔽
	 * 
	 * @param packageName
	 * @return
	 */
	public static boolean isTopActivity(Context context, String packageName) {
		// _context鏄竴涓繚瀛樼殑涓婁笅鏂?		ActivityManager am = (ActivityManager) context.getApplicationContext()
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<ActivityManager.RunningAppProcessInfo> list = am
				.getRunningAppProcesses();
		if (list.size() == 0)
			return false;
		for (ActivityManager.RunningAppProcessInfo process : list) {
			// Log.d(getTAG(), Integer.toString(__process.importance));
			// Log.d(getTAG(), __process.processName);
			if (process.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND
					&& process.processName.equals(packageName)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 妫€娴婣PP鏄惁瀛樺湪
	 * @param context
	 * @param packageName
	 * @return
	 */
	public static boolean checkAppExist(Context context, String packageName) {
		try {
			ApplicationInfo info = context.getPackageManager()
					.getApplicationInfo(packageName, 0);
			return info != null && info.packageName.equals(packageName);
		} catch (PackageManager.NameNotFoundException e) {

		} catch (Exception e) {
		}
		return false;
	}
}