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;
}
批量安装APK
最新推荐文章于 2023-01-12 09:27:16 发布
本文介绍了一种在检测到USB存储已挂载时自动查找并安装特定目录下APK文件的方法。通过线程池执行后台任务,在USB存储上搜索指定路径下的APK文件,并利用Android系统的Intent机制启动应用安装流程。
摘要由CSDN通过智能技术生成