Android calls the system method to obtain application information, such as the cache size.
1. android has a way to get the application cache, but it is a little troublesome to use. However, our applications sometimes need to let users know the size of the cache, so that users can promptly clean up the cache.
Run the Code directly. The demo is created using android studio. Http://pan.baidu.com/s/1sjkONzn
2. The code is just a method.
private void getPackageSizeInfo(final String mPackageName) { try { Method getPackageSizeInfo = getPackageManager().getClass().getMethod( getPackageSizeInfo, String.class, IPackageStatsObserver.class); getPackageSizeInfo.invoke(getPackageManager(), mPackageName, new IPackageStatsObserver.Stub() { @Override public void onGetStatsCompleted(final PackageStats pStats, boolean succeeded) throws RemoteException { runOnUiThread(new Runnable() { @Override public void run() { textView.setText(qq file code + Formatter.formatFileSize(getApplication(), pStats.codeSize) + + data size + Formatter.formatFileSize(getApplication(), pStats.codeSize) + + cache size + Formatter.formatFileSize(getApplication(), pStats.cacheSize) + ); Log.e(content, pStats.toString()); } }); } }); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); }}
Permission required
3. the following uses qq as an example.
getPackageSizeInfo(com.tencent.mobileqq);