Android获取/清除应用数据

代码片段:

import java.lang.reflect.Method;

import android.content.Context;
import android.content.pm.IPackageDataObserver;
import android.content.pm.IPackageManager;
import android.content.pm.IPackageStatsObserver;
import android.content.pm.PackageManager;
import android.content.pm.PackageStats;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;

public class CacheUtils {

    private static final String TAG = CacheUtils.class.getSimpleName();
    private final long MAX_WAIT_TIME = 60 * 1000;
    private final long WAIT_TIME_INCR = 10 * 1000;
    
    /**
     * 获取应用数据大小 cache + data
     * 
     * @param context
     * @param packageName
     * @return
     * @throws Exception
     */

    public long getApplicationCache(Context context, String packageName) throws Exception {
        try {
            PackageStatsObserver observer = new PackageStatsObserver();
            // wait on observer
            synchronized (observer) {
                Method method = PackageManager.class.getMethod("getPackageSizeInfo", new Class[] { String.class, IPackageStatsObserver.class });
                method.invoke(context.getPackageManager(), packageName, observer);
//                getPm().getPackageSizeInfo(packageName, UserHandle.myUserId(), observer);
                long waitTime = 0;
                while ((!observer.isDone()) || (waitTime > MAX_WAIT_TIME)) {
                    observer.wait(WAIT_TIME_INCR);
                    waitTime += WAIT_TIME_INCR;
                }
                if (!observer.isDone()) {
                    throw new Exception("Timed out waiting for PackageStatsObserver.onGetStatsCompleted");
                }
            }
            Log.d(TAG, "toString " + observer.stats.toString());
            return observer.stats.dataSize + observer.stats.cacheSize;
        } catch (RemoteException e) {
            Log.w(TAG, "Failed to get handle for PackageManger Exception: " + e);
            return -1;
        } catch (InterruptedException e) {
            Log.w(TAG, "InterruptedException :" + e);
            return -1;
        }
    }

    /**
     * 清除应用数据
     * 
     * @param context
     * @param packageName
     * @return
     * @throws Exception
     */
    public boolean cleanApplicationUserData(Context context, String packageName) throws Exception {
        try {
            PackageDataObserver observer = new PackageDataObserver();
            // wait on observer
            synchronized (observer) {
                Method method = PackageManager.class.getMethod("clearApplicationUserData", new Class[] { String.class, IPackageDataObserver.class });
                method.invoke(context.getPackageManager(), packageName, observer);
//                getPm().deleteApplicationCacheFiles(appid, observer);
                long waitTime = 0;
                while (!observer.isDone() || (waitTime > MAX_WAIT_TIME)) {
                    observer.wait(WAIT_TIME_INCR);
                    waitTime += WAIT_TIME_INCR;
                }
                if (!observer.isDone()) {
                    throw new Exception("timed out waiting for PackageDataObserver.onRemoveCompleted");
                }
            }
            Log.d(TAG, "cleanApplicationCache " + observer.retValue);
            return observer.retValue;
        } catch (RemoteException e) {
            Log.w(TAG, "Failed to get handle for PackageManger Exception: " + e);
            return false;
        } catch (InterruptedException e) {
            Log.w(TAG, "InterruptedException :" + e);
            return false;
        }
    }

    private IPackageManager getPm() {
        return IPackageManager.Stub.asInterface(ServiceManager.getService("package"));
    }

    class PackageDataObserver extends IPackageDataObserver.Stub {
        public boolean retValue = false;
        private boolean doneFlag = false;

        public void onRemoveCompleted(String packageName, boolean succeeded) throws RemoteException {
            synchronized (this) {
                retValue = succeeded;
                doneFlag = true;
                notifyAll();
            }
        }

        public boolean isDone() {
            return doneFlag;
        }
    }

    class PackageStatsObserver extends IPackageStatsObserver.Stub {
        public boolean retValue = false;
        public PackageStats stats;
        private boolean doneFlag = false;

        public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException {
            synchronized (this) {
                retValue = succeeded;
                stats = pStats;
                doneFlag = true;
                notifyAll();
            }
        }

        public boolean isDone() {
            return doneFlag;
        }
    }

}

AndroidManifest权限要求:


 

 

你可能感兴趣的:(Android)