Android 清除指定应用缓存

/**

* 清除指定应用缓存,即删除data/data/packageName/cache目录下文件

* @param mContext

* @param packageName

*/

public void clearCache(final Context mContext,final String packageName){

(new Thread(){

@Override

publicvoid run() {

try {

Context otherAppContext = mContext.createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY);

File path = otherAppContext.getCacheDir();

if(path ==null)

return;

// #rm -r xxx   删除名字为xxx的文件夹及其里面的所有文件

String killer =" rm -r " + path.toString();

Process p = Runtime.getRuntime().exec("su");

DataOutputStream os =new DataOutputStream(p.getOutputStream());

os.writeBytes(killer.toString() +"\n");

os.writeBytes("exit\n");

os.flush();

} catch (NameNotFoundException e) {

//TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

//TODO Auto-generated catch block

e.printStackTrace();

}

}

}).start();

}

你可能感兴趣的:(Android 清除指定应用缓存)