[英]How to clear other applications cache from another android app in Marshmallow?
I'm trying to develop an android app that could erase others application cache data. 我正在尝试开发一个可以清除其他应用程序缓存数据的android应用程序。 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? 如何从我们的Android应用程序中删除其他应用程序缓存?
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 并在您的src文件夹中创建一个名为
android.content.pm
的包,并在该包中创建一个名为IPackageDataObserver
.aidl的文件,并将以下代码粘贴到其中
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. 上面的代码成功清除了Lollipop,但是当该代码在Marshamallow中执行时,并不能清除所有数据。 Why it is do??????????????
为什么这样做???????????????
Any suggestion will be accepted. 任何建议将被接受。 or another way to solve.
或另一种解决方法。 Thanks.
谢谢。
由于这是一种非官方的方式,因此Google现在已将该方法的签名级别提高到了@marshamallow。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.