Android实现清除应用程序缓存
Posted 且听真言
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android实现清除应用程序缓存相关的知识,希望对你有一定的参考价值。
我使用的是反射的方法来获取某个应用程序的缓存大小,但是没能实现通过反射的方法来清除该应用缓存,所以我只能调用系统的设置意图来清除缓存。而且在真机上没什么问题,模拟器上就有些问题了。
1.需要三个AIDL文件
注意:在添加三个aidl文件后一定要Rebuild Project一下,否则会报错。
1.
IPackageDataObserver.aidl
package android.content.pm; oneway interface IPackageDataObserver void onRemoveCompleted(in String packageName, boolean succeeded);2.
IPackageStatsObserver.aidl
package android.content.pm; import android.content.pm.PackageStats; oneway interface IPackageStatsObserver void onGetStatsCompleted(in PackageStats pStats, boolean succeeded);
3.
PackageStats.aidl
package android.content.pm; parcelable PackageStats;
需要的权限:
<uses-permission android:name="android.permission.GET_PACKAGE_SIZE" /> <uses-permission android:name="android.permission.CLEAR_APP_CACHE" /> <!-- <uses-permission android:name="android.permission.CLEAR_APP_USER_DATA"/> <uses-permission android:name="android.permission.DELETE_CACHE_FILES"/>-->
主要代码:
package zy.just.com.clearcachedemo;
import android.content.Intent;
import android.content.pm.IPackageStatsObserver;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageStats;
import android.net.Uri;
import android.os.Bundle;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.text.format.Formatter;
import android.view.View;
import android.widget.EditText;
import java.io.BufferedInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class MainActivity extends AppCompatActivity
public static final String ENCODING="UTF-8";//常量,代表编码格式。
private EditText et_size;
private PackageManager packageManager;
private PackageInfo packageInfo;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
init();
createCache();
public void createCache()
InputStream in = getResources().openRawResource(R.mipmap.pic_1);
BufferedInputStream bis = new BufferedInputStream(in);
try
FileOutputStream fos = openFileOutput("pic_1.png", MODE_PRIVATE);
byte[] buffer=new byte[1024];
int len = -1;
while ((len=bis.read(buffer))!=-1)
fos.write(buffer,0,len);
fos.close();
bis.close();
catch (FileNotFoundException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
private void init()
packageManager = getPackageManager();
private void initView()
et_size = (EditText) findViewById(R.id.et_size);
public void clear(View view)
// Toast.makeText(MainActivity.this,"点击了clear按钮",0).show();
// try
// Method[] methods = PackageManager.class.getMethods();
// for(Method m:methods)
// if("deleteApplicationCacheFiles".equals(m.getName()))
// m.invoke(packageManager,packageInfo.packageName,new ClearCacheObserver());
//
// if("deleteApplicationCacheFiles".equals(m.getName()))
// m.invoke(packageManager,packageInfo.packageName,new ClearCacheObserver());
//
//
method = PackageManager.class.getDeclaredMethod("clearApplicationUserData",String.class,IPackageDataObserver.class);
method.invoke(packageManager,packageInfo.packageName,new ClearCacheObserver());
method = PackageManager.class.getDeclaredMethod("deleteApplicationCacheFiles",String.class,IPackageDataObserver.class);
method.invoke(packageManager,packageInfo.packageName,new ClearCacheObserver());
// catch (InvocationTargetException e)
// e.printStackTrace();
// catch (IllegalAccessException e)
// e.printStackTrace();
//
Intent intent = new Intent();
intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
intent.addCategory(Intent.CATEGORY_DEFAULT);
// dat=package:com.itheima.mobileguard
intent.setData(Uri.parse("package:" + packageInfo.packageName));
startActivity(intent);
@Override
protected void onResume()
super.onResume();
showCacheSize();
private void showCacheSize()
if(packageManager!=null)
try
packageInfo = packageManager.getPackageInfo("zy.just.com.clearcachedemo", 0);
getCacheSize(packageInfo);
catch (PackageManager.NameNotFoundException e)
e.printStackTrace();
/**
* 获取某个包名对应的应用程序的缓存大小
* @param packageInfo 应用程序的包信息
*
* IPackageStatsObserver.class编译器报找不到的问题,只需要rebuild一下就行了。
*/
private void getCacheSize(PackageInfo packageInfo)
try
//通过反射获取到当前的方法。
Method method = PackageManager.class.getDeclaredMethod("getPackageSizeInfo",String.class,IPackageStatsObserver.class);
/**
* 第一个参数:调用该方法的对象
* 第二个参数:应用包名
* 第三个参数:IPackageStatsObserver类型的aidl对象。
*/
method.invoke(packageManager,packageInfo.packageName,new MyPackObserver(packageInfo));
catch (NoSuchMethodException e)
e.printStackTrace();
catch (InvocationTargetException e)
e.printStackTrace();
catch (IllegalAccessException e)
e.printStackTrace();
private class MyPackObserver extends android.content.pm.IPackageStatsObserver.Stub
private PackageInfo info;
public MyPackObserver(PackageInfo info)
this.info = info;
@Override
public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException
long cacheSize = pStats.cacheSize;
et_size.setText(Formatter.formatFileSize(getApplicationContext(), cacheSize));
// class ClearCacheObserver extends IPackageDataObserver.Stub
// public void onRemoveCompleted(final String packageName, final boolean succeeded)
// getCacheSize(packageInfo);
//
//
以上是关于Android实现清除应用程序缓存的主要内容,如果未能解决你的问题,请参考以下文章