@Override public void onCreate() { try { mGetPackageSizeInfoMethod = getPackageManager().getClass() .getMethod( "getPackageSizeInfo", String.class, IPackageStatsObserver.class); mFreeStorageAndNotifyMethod = getPackageManager().getClass() .getMethod( "freeStorageAndNotify", long.class, IPackageDataObserver.class); mDeleteApplicationCacheFiles = getPackageManager().getClass() .getMethod( "deleteApplicationCacheFiles", String.class, IPackageDataObserver.class); } catch (NoSuchMethodException e) { e.printStackTrace(); } }
@Override protected Void doInBackground(Void... params) { mCallback.onBegin(); PackageManager pm = MyApplication.getInstance().getPackageManager(); List<ApplicationInfo> installedPackages = pm.getInstalledApplications(PackageManager.GET_GIDS); IPackageStatsObserver.Stub observer = new PackageStatsObserver(); mScanCount = 0; mTotalCount = installedPackages.size(); mSysCaches = new ArrayList<>(); mAppNames = new HashMap<>(); for (int i = 0; i < mTotalCount; i++) { ApplicationInfo info = installedPackages.get(i); mAppNames.put(info.packageName, pm.getApplicationLabel(info).toString()); getPackageInfo(info.packageName, observer); } return null; }
/** * 根据包名获取应用程序的体积信息 注意: 这个方法是一个异步的方法 程序的体积要花一定时间才能获取到. * * @param packname */ private void setAppSize(final String packname) { try { Method method = PackageManager.class.getMethod("getPackageSizeInfo", new Class[] { String.class, IPackageStatsObserver.class }); method.invoke(getPackageManager(), new Object[] { packname, new IPackageStatsObserver.Stub() { public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException { // 注意这个操作是一个异步的操作 long codesize = pStats.codeSize; long datasize = pStats.dataSize; Logger.i(TAG, packname); CacheInfo info = maps.get(packname); info.setDataSize(Formatter.formatFileSize(mApp, datasize)); info.setCodeSize(Formatter.formatFileSize(mApp, codesize)); maps.put(packname, info); } } }); } catch (Exception e) { e.printStackTrace(); } }
@Override protected Boolean doInBackground(Void... params) { mCallBack.onScanStart(); ArrayList<CacheInfo> caches = CacheLab.get(mAppContext).getCaches(); caches.clear(); mScanCount = 0; mTotalCount = 0; PackageManager pm = mAppContext.getPackageManager(); List<ApplicationInfo> installedPackages = pm.getInstalledApplications(PackageManager.GET_GIDS); IPackageStatsObserver.Stub observer = new PackageSizeObserver(); mTotalCount = installedPackages.size(); for (int i = 0; i < mTotalCount; i++) { ApplicationInfo appInfo = installedPackages.get(i); // NOTICE!This call is not synchronize getPackageInfo(appInfo.packageName, observer); } return true; }
private void getCaches(){ // scan pm = getPackageManager(); List<PackageInfo> packages = pm.getInstalledPackages(0); int max = packages.size(); int current = 0; sb.delete(0, sb.length()); sb.append(""); sb.append("本机一共安装了:" + max+"个应用"); tvShowCaches.setText(sb.toString()); for (PackageInfo pinfo : packages) { String packageName = pinfo.packageName; try { Method getPackageSizeInfo = PackageManager.class .getDeclaredMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class); getPackageSizeInfo.invoke(pm, packageName, new MyPackageStateObserver()); current++; } catch (Exception e) { current++; e.printStackTrace(); } } //===到这里,数据准备完成 mHadler.postDelayed(new Runnable() { @Override public void run() { btnClearAll.setVisibility(View.VISIBLE); Toast.makeText(getApplicationContext(),"已读取全部缓存",Toast.LENGTH_SHORT).show(); sbCache.append(Formatter.formatFileSize(getApplicationContext(),cacheS)+"\n"); tvShowCaches.setText(sb.toString()); tvAppCache.setText(sbCache.toString()); sbCache.delete(0,sbCache.length()); } }, 1000); //ok,所有应用程序信息显示完成 }
private void getPackageInfo(Context context, String pkg) { PackageManager pm = context.getPackageManager(); try { Method method_getPackageSizeInfo = pm.getClass().getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class); method_getPackageSizeInfo.invoke(pm, pkg, new IPackageStatsObserver.Stub() { @Override public IBinder asBinder() { log.d("asBinder"); return super.asBinder(); } @Override public void onGetStatsCompleted(PackageStats packageStats, boolean b) throws RemoteException { final StringBuilder sb = new StringBuilder("onGetStatsCompleted\n"); sb.append("packageName:").append(packageStats.packageName).append("\n"); sb.append("cacheSize:").append(packageStats.cacheSize).append("\n"); sb.append("dataSize:").append(packageStats.dataSize).append("\n"); sb.append("externalDataSize:").append(packageStats.externalDataSize).append("\n"); log.i(sb.toString()); mHandler.post(new Runnable() { @Override public void run() { mDataBinding.txtContent.setText(sb); } }); } }); } catch (Exception e) { e.printStackTrace(); } }
/** * 获取某个包名对应的应用程序的缓存大小 * @param */ public void getCacheSize(PackageInfo info) { try { Method method = PackageManager.class.getDeclaredMethod( "getPackageSizeInfo",String.class,IPackageStatsObserver.class); method.invoke(packageManager,info.packageName,new MyPackObserver(info)); } catch (Exception e) { e.printStackTrace(); } }
@Override public void onCreate(Bundle savedInstanceState) { //position = mContext.getArguments().getInt(ARG_POSITION); try { mGetPackageSizeInfoMethod = mContext.getPackageManager() .getClass() .getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class); } catch (NoSuchMethodException e) { e.printStackTrace(); } //mTaskScanApps = new TaskScanApps(); //mAppsView.initViews(mAppsListAdapter,mContext,); }
/** * get app size by package name * @param context * @param packageName package name * @param listener it will be call when success to get size */ public static void getAppSize(Context context, String packageName, final AppSizeInfoListener listener) { // check argument if(null == listener) { throw new NullPointerException("listener can't be null"); } if(TextUtils.isEmpty(packageName)) { throw new IllegalArgumentException("packageName can't be empty"); } // get pm PackageManager pm = context.getPackageManager(); Method getPackageSizeInfo = null; try { // get method getPackageSizeInfo getPackageSizeInfo = pm.getClass().getMethod( "getPackageSizeInfo", String.class, IPackageStatsObserver.class); // call method getPackageSizeInfo.invoke(pm, packageName, new IPackageStatsObserver.Stub() { @Override public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException { // call listener listener.onGetSizeInfoCompleted( new AppSizeInfo(pStats.cacheSize, pStats.dataSize, pStats.codeSize)); } }); } catch (Exception e) { e.printStackTrace(); } }
@Override public void onCreate() { try { mGetPackageSizeInfoMethod = getPackageManager().getClass() .getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class); mFreeStorageAndNotifyMethod = getPackageManager().getClass() .getMethod("freeStorageAndNotify", long.class, IPackageDataObserver.class); } catch (NoSuchMethodException e) { e.printStackTrace(); } }
@Override protected void initViews() { mContext = getActivity(); try { mGetPackageSizeInfoMethod = mContext.getPackageManager().getClass().getMethod( "getPackageSizeInfo", String.class, IPackageStatsObserver.class); } catch (NoSuchMethodException e) { e.printStackTrace(); } }
public static long getPkgSize(final Context context, String pkgName) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { long pkgSize=0; // getPackageSizeInfo是PackageManager中的一个private方法,所以需要通过反射的机制来调用 Method method = PackageManager.class.getMethod("getPackageSizeInfo", new Class[]{String.class, IPackageStatsObserver.class}); // 调用 getPackageSizeInfo 方法,需要两个参数:1、需要检测的应用包名;2、回调 method.invoke(context.getPackageManager(), new Object[]{ pkgName, new IPackageStatsObserver.Stub() { @Override public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException { // 子线程中默认无法处理消息循环,自然也就不能显示Toast,所以需要手动Looper一下 Looper.prepare(); // 从pStats中提取各个所需数据 // pkgSize= pStats.cacheSize+pStats.dataSize+pStats.codeSize; // Toast.makeText(context, // "缓存大小=" + Formatter.formatFileSize(context, pStats.cacheSize) + // "\n数据大小=" + Formatter.formatFileSize(context, pStats.dataSize) + // "\n程序大小=" + Formatter.formatFileSize(context, pStats.codeSize), // Toast.LENGTH_LONG).show(); // 遍历一次消息队列,弹出Toast Looper.loop(); } } }); return pkgSize; }
private void getCacheInfo(PackageInfo packageInfo) { try { /** * ����API */ Method method = PackageManager.class.getDeclaredMethod( "getPackageSizeInfo", String.class, IPackageStatsObserver.class); method.invoke(pm, packageInfo.applicationInfo.packageName, new MyIPackageStatsObserver(packageInfo)); } catch (Exception e) { e.printStackTrace(); } }
/** ��ȡָ�����Ĵ�С��Ϣ **/ public void queryPackageSize(String packageName) throws Exception { Log.i(TAG, "packageName:" + packageName); if (packageName != null) { // ʹ�÷�����Ƶõ�PackageManager������غ���getPackageSizeInfo PackageManager pManager = getPackageManager(); //ͨ��������ƻ�ø����غ��� Method getPackageSizeInfo = pManager.getClass().getMethod("getPackageSizeInfo", String.class,IPackageStatsObserver.class); //���øú��������Ҹ��������� ��������������ɺ��ص�PkgSizeObserver��ĺ��� getPackageSizeInfo.invoke(pManager, packageName,new PkgSizeObserver()); } }
/** * Gets the package stats of the given application. * The package stats are getting sent via OTTO * * @param pkg The package name of the application */ public static void getSize(final PackageManager pm, final PackageStatsObserver.OnPackageStatsListener l, final String pkg) { try { Method m = pm.getClass().getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class); m.invoke(pm, pkg, new PackageStatsObserver(l)); } catch (Exception e) { Timber.e(e, "AppHelper.getSize()"); } }
public void getPackageInfo(String packageName, IPackageStatsObserver.Stub observer) { try { PackageManager pm = mAppContext.getPackageManager(); Method getPackageSizeInfo = pm.getClass() .getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class); getPackageSizeInfo.invoke(pm, packageName, observer); } catch (Exception e) { e.printStackTrace(); } }
@Override public void onCreate() { try { mGetPackageSizeInfoMethod = getPackageManager().getClass().getMethod( "getPackageSizeInfo", String.class, IPackageStatsObserver.class); mFreeStorageAndNotifyMethod = getPackageManager().getClass().getMethod( "freeStorageAndNotify", long.class, IPackageDataObserver.class); } catch (NoSuchMethodException e) { e.printStackTrace(); } }
/** @hide */ @Override public void getPackageSizeInfoAsUser(String packageName, int userId, IPackageStatsObserver observer) { mBase.getPackageSizeInfoAsUser(packageName, userId, observer); }
/** @hide */ @Override public void getPackageSizeInfo(String packageName, IPackageStatsObserver observer) { mBase.getPackageSizeInfo(packageName, observer); }
@Override protected List<AppsListItem> doInBackground(Void... params) { mCacheSize = 0; final List<ApplicationInfo> packages = getPackageManager().getInstalledApplications( PackageManager.GET_META_DATA); publishProgress(0, packages.size()); final CountDownLatch countDownLatch = new CountDownLatch(packages.size()); final List<AppsListItem> apps = new ArrayList<>(); try { for (ApplicationInfo pkg : packages) { mGetPackageSizeInfoMethod.invoke(getPackageManager(), pkg.packageName, new IPackageStatsObserver.Stub() { @Override public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException { synchronized (apps) { publishProgress(++mAppCount, packages.size()); mCacheSize += addPackage(apps, pStats, succeeded); } synchronized (countDownLatch) { countDownLatch.countDown(); } } } ); } countDownLatch.await(); } catch (InvocationTargetException | InterruptedException | IllegalAccessException e) { e.printStackTrace(); } return new ArrayList<>(apps); }
@Override public void getPackageSizeInfo(String packageName, int userHandle, IPackageStatsObserver observer) { }