安卓开发中如何在本程序中删除其他程序的外部缓存


拜托有知道的大神告诉我一下,急用

Android 缓存

梦里的大肉 10 years, 2 months ago

你指的是删除其他应用的数据吗?如果是的话,先停止该应用,再删


 public void deleAppData(String packageName) {
        PackageManager packageManager = mContext.getPackageManager();
        packageManager.clearApplicationUserData(packageName, new IPackageDataObserver.Stub() {

            public void onRemoveCompleted(String arg0, boolean arg1)
                    throws RemoteException {

            }
        });
    }

熊猫猫lx answered 10 years, 2 months ago

我觉得这是一件很恐怖的事情

魔理沙的弟弟 answered 10 years, 2 months ago


 java


 PackageManager pm = context.getPackageManager();
            Class[] arrayOfClass = new Class[2];
            Class localClass2 = Long.TYPE;
            arrayOfClass[0] = localClass2;
            arrayOfClass[1] = IPackageDataObserver.class;
            Method localMethod = pm.getClass().getMethod(
                    "freeStorageAndNotify", arrayOfClass);
            Long localLong = Long.valueOf(getEnvironmentSize() - 1L);
            Object[] arrayOfObject = new Object[2];
            arrayOfObject[0] = localLong;
            localMethod.invoke(pm, localLong, new IPackageDataObserver.Stub() {
                public void onRemoveCompleted(String packageName,
                        boolean succeeded) throws RemoteException {
                }
            });

关键代码在这里了,没别的,自己try,catch一下。
另外,这是清除所有程序的缓存的。若想清除指定应用缓存是必须root的,否则你就跳应用详情页让用户手动清除吧。

我就是⑨~ answered 10 years, 2 months ago

你说的其他程序的外部缓存,是指Environment.getExternalStorageDirectory()目录下的吧?

Tazusa answered 10 years, 2 months ago

Your Answer