原文地址


通过PackageManager获得已安装程序:

java代码:
List installedList = mPackageManager.getInstalledPackages(0);

for (PackageInfo info : installedList) {
mInstalledList.add(info.packageName + ":" + info.applicationInfo.publicSourceDir + ":" + info.application.SourceDir);
count++;
}


packageName取得应用图标应用名称等等;通过PackageInfo的applicationInfo的publicSourceDir获得路径,再通过该路径创建一个文件new File(String dir),得到该文件长度除以1024则取得该应用的大小。
1)取得程序大小,通过application的publicSourceDir获得。
2)取得程序时间,通过application的SourceDir获得。

java代码:
new Date(new File(fileDir).lastModified()).toGMTString();


通过PackageManager类的getInstalledApplications方法返回一个ApplicationInfo数组,ApplicationInfo类中sourceDir可以获取APK的文件路径,从而使用File类读取文件的上次修改时间而实现。但这可能导致:

1. 无法获取原始的创建时间,可能很早就被创建了,之后被替换了。
2. 如果这个APK在一个私有的位置,比如app-private目录,使用Market付费购买的应用在这个位置,如果没有Root的Android手机是没有权限读取的,也导致获取时间失败。

在Android 2.3 API Level为9中,ApplicationInfo类新增的firstInstallTime和lastUpdateTime这两个字段,可以直接获取到APK的创建或上次修改的时间,即使是付费软件也能正常的获取。

java代码:
/**
* 获取已经安装的软件列表
* @param getSysPackages
* @return
*/

public static ArrayListgetInstalledApps(){
ArrayListres = new ArrayList();
ListinstalledAppList = mPackageManager.getInstalledApplications(
PackageManager.GET_UNINSTALLED_PACKAGES);
if (installedAppList == null) {
return null;
}
ListappList =new ArrayList();
for (ApplicationInfo appInfo : installedAppList) {
boolean flag = false;
if ((appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
flag = true;
} else if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
flag = true;
}
if (flag) {
appList.add(appInfo);
}
}

for(int i=0;i ApplicationInfo p = appList.get(i);
String dir = p.publicSourceDir;
int size = Integer.valueOf((int) new File(dir).length());
String date = new Date(new File(dir).lastModified()).toGMTString();
MyApplicationInfo newInfo = new MyApplicationInfo();
newInfo.appName = p.loadLabel(mPackageManager).toString();
newInfo.packageName = p.packageName;
newInfo.size = round(size);


try {
newInfo.versionName =mPackageManager.getPackageInfo(newInfo.packageName, 0).versionName;
newInfo.versionCode =String.valueOf(mPackageManager.getPackageInfo(newInfo.packageName, 0).versionCode);
} catch (Exception e) {
Log.e("PackageUtil", e.getMessage());
}
newInfo.icon = p.loadIcon(mPackageManager);
res.add(newInfo);
}
return res;
}

============================华丽的分割线============================================

上面的好像都不能用,在StackOverflow上找到一个方案:

public static long getApkUpdateTime(Context context) {PackageManager pm = context.getPackageManager();ZipFile zf = null;try {PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);zf = new ZipFile(packageInfo.applicationInfo.sourceDir);ZipEntry ze = zf.getEntry("classes.dex");return ze.getTime();} catch (NameNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (zf != null) {try {zf.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}return 0;}

或者这个:

public static long getApkUpdateTime2(Context context) {ZipFile zf = null;try {ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0);zf = new ZipFile(ai.sourceDir);ZipEntry ze = zf.getEntry("META-INF/MANIFEST.MF");return ze.getTime();} catch (Exception e) {} finally {if (zf != null) {try {zf.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}return 0;}


更多相关文章

  1. Android - Toast自定义显示时间,以及时长
  2. Android代码总结,Sdcard和图片相关
  3. android中选择文件,部分手机找不到文件路径问题的解决
  4. android 下载不了源代码?
  5. Android调用相机拍照并返回路径和调用系统图库选择图片
  6. android 获取配置文件 相对路径
  7. android 2.2 完全 退出 ---程序代码
  8. Android 功能代码总结

随机推荐

  1. Android(安卓)Matrix处理ImageView中图片
  2. 关于同步加载和异步加载
  3. 玩懂Log,打开Android大门(sundy深入浅出)之
  4. Android实现dialog的3D翻转
  5. Lottie开源动画库使用教程
  6. Android(安卓)UI效果实现——滑动模糊渐
  7. Android中是否推荐使用枚举Enum
  8. ActivityThread的main方法究竟做了什么?
  9. Android属性动画之第一重修炼总结
  10. Android亮屏和熄屏控制