private List<MyFile> myFiles = new ArrayList<MyFile>();
public List<MyFile> getMyFiles() {
return myFiles;
}
/**
* @param args
* 运用递归的思想,递归去找每个目录下面的apk文件
* @return
* @throws Exception
*/
public List<MyFile> FindAllAPKFile(File file) throws Exception {
// 手机上的文件,目前只判断SD卡上的APK文件
// file = Environment.getDataDirectory();
// SD卡上的文件目录
if (file.isFile()) {
String name_s = file.getName();
MyFile myFile = new MyFile();
String apk_path = null;
// MimeTypeMap.getSingleton()
if (name_s.toLowerCase().endsWith(".apk")) {
apk_path = file.getAbsolutePath();// apk文件的绝对路劲
// System.out.println("----" + file.getAbsolutePath() + "" +
// name_s);
PackageManager pm = context.getPackageManager();
PackageInfo packageInfo = pm.getPackageArchiveInfo(apk_path, PackageManager.GET_ACTIVITIES);
ApplicationInfo appInfo = packageInfo.applicationInfo;
//apk or pkg?
myFile.setAppOrPkg(0);
/**获取apk的图标 */
appInfo.sourceDir = apk_path;
appInfo.publicSourceDir = apk_path;
Drawable apk_icon = appInfo.loadIcon(pm);
myFile.setApk_icon(apk_icon);
/** 得到包名 */
String packageName = packageInfo.packageName;
myFile.setPackageName(packageName);
String appname = appInfo.loadLabel(pm).toString();
myFile.setAppName(appname);
/** apk的绝对路劲 */
myFile.setFilePath(file.getAbsolutePath());
/** apk的版本名称 String */
String versionName = packageInfo.versionName;
myFile.setVersionName(versionName);
/** apk的版本号码 int */
int versionCode = packageInfo.versionCode;
myFile.setVersionCode(versionCode);
myFiles.add(myFile);
} else {
File[] files = file.listFiles();
if (files != null && files.length > 0) {
for (File file_str : files) {
FindAllAPKFile(file_str);
}
}
}
return myFiles;
}
网友评论