批量安装APK

本文介绍了一种在检测到USB存储已挂载时自动查找并安装特定目录下APK文件的方法。通过线程池执行后台任务,在USB存储上搜索指定路径下的APK文件,并利用Android系统的Intent机制启动应用安装流程。
摘要由CSDN通过智能技术生成
private static boolean isUSBStorageMount() {

		File file = new File(USBBroadcastReceiver.USB_PATH);
		if (file.exists()) {
			return true;
		}
		return false;

	}

	// public boolean getPreferences(){
	// SharedPreferences preferences = getSharedPreferences("OttLoder",
	// MODE_WORLD_READABLE);
	// return preferences.getBoolean("isLoader", false);
	// }

	ExecutorService installAPKThread = null;

	private void findUserAPK() {

		final String preinstallAPKDir = "/storage/external_storage/sda1/yotv_apks";
		// TODO Auto-generated method stub
		if (isUSBStorageMount()) {
			if (installAPKThread == null) {
				installAPKThread = Executors.newFixedThreadPool(1);
			}

			installAPKThread.execute(new Runnable() {

				@Override
				public void run() {

					if (isUSBStorageMount()) {
						File usbfile = new File(USBBroadcastReceiver.USB_PATH);

						try {
							File[] usbFileList = usbfile.listFiles();
							for (File tmp : usbFileList) {
								if (tmp.isDirectory() && tmp.getAbsolutePath().toString().equals(preinstallAPKDir)) {
									File[] apkFileList = tmp.listFiles();
									for (File apkFile : apkFileList) {
										Log.d("pin5", "findUserAPK***find pre apkName---->" + apkFile.getName());
										installAPK(apkFile.getAbsoluteFile());
									}
								}
							}
						} catch (Exception e) {
							// TODO: handle exception
						}
					}
				}

			});

		} else {
			Log.d("pin5", "**********findUserAPK file is null,not usb mount***************");
		}

	}

	private void installAPK(File file) {

		// TODO Auto-generated method stub

		if (file.toString() != null) {
			if (getMIMEType(file).equals("apk")) {
				if (getUnInstallAPKInfo(file.toString(), this) == null) {
					Log.d("pin5", "**********go installAPK file is:" + file);
					Intent intent = new Intent();
					intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					intent.setAction(android.content.Intent.ACTION_VIEW);
					intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
					startActivity(intent);
					// finish();
				}
			}
		}
	};

	/**
	 * @Title: getInstallAPKInfo
	 * @Description:
	 * @param :
	 * @return versionName
	 * @throws
	 */
	public String getInstallAPKInfo(Context context, String pkg) {

		PackageManager pm = getPackageManager();
		PackageInfo packageInfo = null;
		String flag = null;
		try {
			packageInfo = pm.getPackageInfo(pkg, PackageManager.GET_ACTIVITIES);
			if (packageInfo != null) {
				flag = packageInfo.versionName;
			} else {
				flag = null;
			}
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			return flag;
		}
	}

	/**
	 * @Title: getUnInstallAPKInfo
	 * @Description: null ,have install,while :not null ,have not install
	 * @param :
	 * @return versionName
	 * @throws
	 */
	public String getUnInstallAPKInfo(String absPath, Context context) {

		PackageManager pm = getPackageManager();
		PackageInfo packageInfo = pm.getPackageArchiveInfo(absPath,
				PackageManager.GET_ACTIVITIES);
		if (packageInfo != null) {
			String unInstallVersionName = packageInfo.versionName.toString();
			 Log.i("pin5", "getUnInstallAPKInfo applicationInfo.packageName->"
			 + packageInfo.packageName);
			 Log.i("pin5", "getUnInstallAPKInfo applicationInfo.versionCode->"
			 + packageInfo.versionCode);
			String installVersionName = getInstallAPKInfo(this, packageInfo.packageName);
			if ((installVersionName == null) || (!unInstallVersionName.equals(installVersionName))) {
				return null;
			} else {
				return packageInfo.versionName;
			}

		}
		return null;
	}

	/**
	 * @Title: getMIMEType
	 * @Description: file extend type:MimeType
	 * @param :
	 * @return String
	 * @throws
	 */
	private String getMIMEType(File file) {

		String type = "";
		String filename = file.getName();
		type = filename.substring(filename.lastIndexOf(".") + 1, filename.length());
		return type;

	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值