How to clear other applications cache from another android app in Marshmallow?

独自空忆成欢 提交于 2019-12-12 21:03:12

问题


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 am able to clear my application's cache by the following code

Reference :
How to delete other applications cache from our android app?

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


And also create a package 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"/>

Above codes successfully cleared up to Lollipop but when this code execute in Marshamallow, then it is not clear all data. Why it is do??????????????

Any suggestion will be accepted. or another way to solve. Thanks.


回答1:


Because it's a non-official way of doing it and Google has raised the method's signature level now to signature|system @ Marshamallow.



来源:https://stackoverflow.com/questions/36905938/how-to-clear-other-applications-cache-from-another-android-app-in-marshmallow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!