List<PackageInfo> packages = getPackageManager().getInstalledPackages(0);
public PkgSizeObserver pkgsizeobserver = new PkgSizeObserver();
try { Method method; method = this.getPackageManager().getClass().getMethod( "getPackageSizeInfo", new Class[] { String.class, IPackageStatsObserver.class }); method.invoke(this.getPackageManager(), packageInfo.packageName, pkgsizeobserver); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } synchronized (pkgsizeobserver) { try { pkgsizeobserver.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } totalApp = pkgsizeobserver.cachesize + pkgsizeobserver.codesize + pkgsizeobserver.datasize;
//<span style="font-family: Arial, Helvetica, sans-serif;">PkgSizeObserver.class</span>
//aidl文件形成的Bindler机制服务类 public class PkgSizeObserver extends IPackageStatsObserver.Stub { public long cachesize = 0; public long datasize = 0; public long codesize = 0; /*** * 回调函数, * * @param pStatus * ,返回数据封装在PackageStats对象中 * @param succeeded * 代表回调成功 */ @Override public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException { // TODO Auto-generated method stub if(succeeded) { cachesize = pStats.cacheSize; // 缓存大小 datasize = pStats.dataSize; // 数据大小 codesize = pStats.codeSize; // 应用程序大小 synchronized (this) { this.notifyAll(); } } } }
原文地址:http://blog.csdn.net/woyaochenggong774/article/details/46679881