如何从Marshmallow的另一个Android应用程序清除其他应用程序缓存? [英] How to clear other applications cache from another android app in Marshmallow?

查看:218
本文介绍了如何从Marshmallow的另一个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 am able to clear my application's cache by the following code

参考:
如何从我们的Android应用程序中删除其他应用程序缓存?

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



另外在src文件夹里创建一个名为 android.content.pm 在该包中创建名为 IPackageDataObserver .aidl的文件,然后粘贴以下代码


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"/>

以上代码已成功清除到Lollipop,但是当该代码在Marshamallow中执行时,则不清楚所有数据。为什么这样做?

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.

推荐答案

由于这是一种非官方的方式,Google已经将方法的签名级别提升到签名系统@ Marshamallow。

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

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

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