如何从我们的 android 应用程序中删除其他应用程序缓存? [英] How to delete other applications cache from our android app?

查看:21
本文介绍了如何从我们的 android 应用程序中删除其他应用程序缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个可以擦除其他应用程序缓存数据的 Android 应用程序,我尝试浏览所有博客,但没有一个对我有用,我可以通过以下代码清除我的应用程序缓存

I'm trying to develop an android app that could erase others application cache data, I tried to browse through all blogs but none of them worked for me, I can able to clear my application's cache by the following code

File cache = getCacheDir();
            File appDir = new File(cache.getParent());
            if (appDir.exists()) 
            {
                String[] children = appDir.list();
                for (String s : children) 
                {
                    if (!s.equals("lib"))
                    {
                        deleteDir(new File(appDir, s));
                        Toast.makeText(DroidCleaner.this, "Cache Cleaned", Toast.LENGTH_LONG).show();
                        Log.i("TAG", "**************** File /data/data/APP_PACKAGE/" + s + " DELETED *******************");
                    }
                }
            }

public static boolean deleteDir(File dir) 
{
    if (dir != null && dir.isDirectory()) 
    {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) 
        {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) 
            {
                return false;
            }
        }
    }
    return dir.delete();
}

我的清单代码

<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>

我在 2.2、2.3 和 4.0 上测试了代码

I tested the code on 2.2, 2.3 and 4.0

在看到以下链接中的帖子后Android:清除所有应用程序的缓存?

and after seeing the post in the following link Android: Clear Cache of All Apps?

我将代码更改为

PackageManager  pm = getPackageManager();
// Get all methods on the PackageManager
Method[] methods = pm.getClass().getDeclaredMethods();
for (Method m : methods) {
    if (m.getName().equals("freeStorage")) {
        // Found the method I want to use
        try {
            long desiredFreeStorage = 8 * 1024 * 1024 * 1024; // Request for 8GB of free space
        m.invoke(pm, desiredFreeStorage , null);
    } catch (Exception e) {
        // Method invocation failed. Could be a permission problem
    }
    break;
   }
}

我想清除其他应用程序的缓存,任何人都可以帮助我,如果我错了,请纠正我,在此先感谢.

I want to clear other application's cache, can any body please help me, please correct me if I'm wrong, Thanks in advance.

推荐答案

API 23 不再支持此 API,即 Marshmallow.权限在 Marshmallow 中已弃用.

This API is no more supported in API 23, that is Marshmallow. Permission is deprecated in Marshmallow.

但还有另一种方法,即请求附件的运行时许可.试用应用 多合一工具箱 从游戏商店.即使在 Marshmallow 中,此应用程序也能够清除其他应用程序缓存.那么我们应该可以这样做.

But there is another way by asking run time permission for Accessories. Try app All-in-one Toolbox from play store. This app is able to clear other apps cache even in Marshmallow. Then it should be possible for us to do so.

我正在研究这个.找到解决方案后,我将更新答案.谢谢.

I am researching on this. Once I found the solution, I will update the answer. Thanks.

旧答案如下

<小时>

我使用了以下代码,现在我无需root即可清除所有应用程序的缓存,它对我来说非常适合,


I used the following code and now I'm able to clear all application's cache without rooting, it's working perfectly for me,

private static final long CACHE_APP = Long.MAX_VALUE;
private CachePackageDataObserver mClearCacheObserver;

btnCache.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            clearCache();
        }
    });//End of btnCache Anonymous class

void clearCache() 
{
    if (mClearCacheObserver == null) 
    {
      mClearCacheObserver=new CachePackageDataObserver();
    }

    PackageManager mPM=getPackageManager();

    @SuppressWarnings("rawtypes")
    final Class[] classes= { Long.TYPE, IPackageDataObserver.class };

    Long localLong=Long.valueOf(CACHE_APP);

    try 
    {
      Method localMethod=
          mPM.getClass().getMethod("freeStorageAndNotify", classes);

      /*
       * Start of inner try-catch block
       */
      try 
      {
        localMethod.invoke(mPM, localLong, mClearCacheObserver);
      }
      catch (IllegalArgumentException e) 
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      catch (IllegalAccessException e) 
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      catch (InvocationTargetException e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      /*
       * End of inner try-catch block
       */
    }
    catch (NoSuchMethodException e1)
    {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
}//End of clearCache() method

private class CachePackageDataObserver extends IPackageDataObserver.Stub 
{
    public void onRemoveCompleted(String packageName, boolean succeeded) 
    {

    }//End of onRemoveCompleted() method
}//End of CachePackageDataObserver instance inner class

并且还在您的 src 文件夹中创建一个名为 android.content.pm 的包在该包内创建一个名为 IPPackageDataObserver.aidl<的文件/code> 并将以下代码粘贴到其中

And also create a pacakge in your src folder with the name android.content.pm inside that package create a file in the name IPackageDataObserver.aidl and paste the following code to it

package android.content.pm;

/**
 * API for package data change related callbacks from the Package Manager.
 * Some usage scenarios include deletion of cache directory, generate
 * statistics related to code, data, cache usage(TODO)
 * {@hide}
 */
oneway interface IPackageDataObserver {
    void onRemoveCompleted(in String packageName, boolean succeeded);
}

并在您的清单中确保您使用了以下代码

and in your manifest make sure you used the following code

<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>

如果你们发现任何问题,请随时与我联系,谢谢.

If you guys find any problem feel free to contact me, Thanks.

这篇关于如何从我们的 android 应用程序中删除其他应用程序缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆