96利用反射获取缓存和应用程序的大小信息

来源:互联网 发布:js中的addlistenevent 编辑:程序博客网 时间:2024/05/16 11:57

getCacheDir()方法得到缓存的位置。观察系统设置源码看下他的原理:

打开设置的源码:


得到:




继续:


得到:



继续:



得到:



继续:



点一下看到其是类的成员变量,ctrl+k:得到:



打开:发现字符串的值居然是:正在计算...这不是想要的,继续找:



最后我们看到了:





我们需要:

getPackageSizeInfo()但是查看源码:

  /**     * Retrieve the size information for a package.     * Since this may take a little while, the result will     * be posted back to the given observer.  The calling context     * should have the {@link android.Manifest.permission#GET_PACKAGE_SIZE} permission.     *     * @param packageName The name of the package whose size information is to be retrieved     * @param userHandle The user whose size information should be retrieved.     * @param observer An observer callback to get notified when the operation     * is complete.     * {@link android.content.pm.IPackageStatsObserver#onGetStatsCompleted(PackageStats, boolean)}     * The observer's callback is invoked with a PackageStats object(containing the     * code, data and cache sizes of the package) and a boolean value representing     * the status of the operation. observer may be null to indicate that     * no callback is desired.     *     * @hide     */    public abstract void getPackageSizeInfo(String packageName, int userHandle,            IPackageStatsObserver observer);


发现他是隐藏的。

最终实现的代码如下:

package com.ustc.mobilemanager;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.List;import android.app.Activity;import android.content.pm.IPackageStatsObserver;import android.content.pm.PackageInfo;import android.content.pm.PackageManager;import android.content.pm.PackageStats;import android.os.Bundle;import android.os.RemoteException;import android.text.format.Formatter;import android.view.Window;import android.widget.ProgressBar;import android.widget.TextView;public class CleanCacheActivity extends Activity {private ProgressBar progressBar1;private TextView tv_scan_status;private PackageManager pm;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_clean_cache);tv_scan_status = (TextView) findViewById(R.id.tv_scan_status);progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);scanCache();}/** *  * 扫描手机里面所有应用程序的缓存信息 *  */private void scanCache() {pm = getPackageManager();new Thread(new Runnable() {@Overridepublic void run() {Method getPackageSizeInfo = null;Method[] methods = PackageManager.class.getMethods();for (Method method : methods) {System.out.println(method.getName());if ("getPackageSizeInfo".equals(method.getName())) {getPackageSizeInfo = method;}}List<PackageInfo> packInfos = pm.getInstalledPackages(0);for (PackageInfo packageInfo : packInfos) {//public abstract void getPackageSizeInfo(String packageName, int userHandle, IPackageStatsObserver observer);try {getPackageSizeInfo.invoke(pm, packageInfo.packageName,new MyDataObserver());} catch (Exception e) {e.printStackTrace();}}}}).start();}private class MyDataObserver extends IPackageStatsObserver.Stub{@Overridepublic void onGetStatsCompleted(PackageStats pStats, boolean succeeded)throws RemoteException {long cache = pStats.cacheSize;long code = pStats.codeSize; long data = pStats.dataSize; System.out.println("cache:" + Formatter.formatFileSize(getApplicationContext(), cache)); System.out.println("code:" + Formatter.formatFileSize(getApplicationContext(), code)); System.out.println("data:" + Formatter.formatFileSize(getApplicationContext(), data)); String packname = pStats.packageName; System.out.println(packname); System.out.println("------------------");}}}


运行需要下面的权限:





2015了,新年快乐!继续加油。



0 0
原创粉丝点击