2012年3月8日 星期四

本地和設備間傳遞檔案

從設備取得檔案

adb pull /data/data/com.example.sample   ./sample/

從本機上傳檔案
adb push ./sample/  /sdcard/sample/

當然也可以用DDMS,但是有時候指令是方便許多~


參考

2012年3月4日 星期日

安裝程式按安裝時間排序

結合「取得安裝程式列表」和「取得安裝程式時間」這兩個功能就可以完成。

取得安裝程式列表:
private void bindAllApps() {
        
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        mAllApps = mPackageManager.queryIntentActivities(mainIntent, 0);    
        sortappInstalledTime(mAllApps);
    }

根據安裝時間排序:
public void sortappInstalledTime(List<ResolveInfo> mAllApps2) {
        Collections.sort(mAllApps2, new Comparator<Object>() {
            public int compare(Object o1, Object o2) {
                ResolveInfo p1 = (ResolveInfo) o1;
                ResolveInfo p2 = (ResolveInfo) o2;
                Date d1 = getInstallTime(p1.activityInfo.packageName);
                Date d2 = getInstallTime(p2.activityInfo.packageName);

                if (d1 != null && d2 != null) {
                    if (d1.after(d2)) {
                        return -1;
                    } else if (d1 == d2) {
                        return 0;
                    } else {
                        return 1;
                    }
                } else {
                    return 1;
                }
            }
        });
    }

取得安裝時間:
    public Date getInstallTime(String packageName) {
        PackageManager pkgMgt = this.getPackageManager();
        return firstNonNull(installTimeFromPackageManager(pkgMgt, packageName),
                apkUpdateTime(pkgMgt, packageName));
    }

    private Date apkUpdateTime(PackageManager packageManager, String packageName) {

        try {
            android.content.pm.ApplicationInfo info = packageManager
                    .getApplicationInfo(packageName, 0);
            File apkFile = new File(info.sourceDir);
            return apkFile.exists() ? new Date(apkFile.lastModified()) : null;
        } catch (NameNotFoundException e) {
            return null; // package not found
        }
    }

    private Date installTimeFromPackageManager(PackageManager packageManager,
            String packageName) {
        try {
            PackageInfo info = packageManager.getPackageInfo(packageName, 0);
            Field field = PackageInfo.class.getField("firstInstallTime");
            long timestamp = field.getLong(info);
            return new Date(timestamp);
        } catch (NameNotFoundException e) {
            return null; // package not found
        } catch (IllegalAccessException e) {
        } catch (NoSuchFieldException e) {
        } catch (IllegalArgumentException e) {
        } catch (SecurityException e) {
        }
        // field wasn't found
        return null;
    }

            private Date firstNonNull(Date... dates) {
                for (Date date : dates)
                    if (date != null)
                        return date;
                return null;
            }

20120702補充:
後來發現,如果你像我一樣是要迴圈取得多個apk來排序的話,這樣的方式效能很x。可改用uid來排序,可以達到一樣的效果。
根據uid大小排序:
public void sortappByUid(List<ResolveInfo> mAllApps2) {
        Collections.sort(mAllApps2, new Comparator<ResolveInfo>() {
            public int compare(ResolveInfo o1, ResolveInfo o2) {
                ResolveInfo r1 = (ResolveInfo) o1;
                ResolveInfo r2 = (ResolveInfo) o2;
                int uid1 = r1.activityInfo.applicationInfo.uid;
                int uid2 = r2.activityInfo.applicationInfo.uid;

                
                    if (uid1>=uid2) {
                        return -1;
                    } else if (r1 == r2) {
                        return 0;
                    } else {
                        return 1;
                    }
                
            }
        });
    }