android data 删除吗,android 6.0 /data/data/packagename/ 目录 删除文件小结

最近遇到一个问题,需要清空应用缓存以及数据的时候发现不能清理彻底。

我是调用了context.getCacheDir() 以及 context.getFilesDir() 后遍历删除的。

发现只能删掉app创建的文件,而第三方sdk建立的文件仍然在那里。

通过getFilesDir()获取到路径后,再getParent获取到data/data/packagename 路径再去做遍历递归删除。

在android6.0的设备上调用getDataDir() 总是提示我 ‘No virtual method getDataDir()...’

明明是有这个方法的,去查了下方法说明

有这么一句

‘Apps should not use this path directly;’

/**

* Returns the absolute path to the directory on the filesystem where all

* private files belonging to this app are stored. Apps should not use this

* path directly; they should instead use {@link #getFilesDir()},

* {@link #getCacheDir()}, {@link #getDir(String, int)}, or other storage

* APIs on this class.

*

* The returned path may change over time if the calling app is moved to an

* adopted storage device, so only relative paths should be persisted.

*

* No additional permissions are required for the calling app to read or

* write files under the returned path.

*

* @see ApplicationInfo#dataDir

*/

public abstract File getDataDir();

顺带着研究了下新版本data/data 相关知识。

因为android 6.0开始支持多用户,所以譬如说getFilesDir()之类的方法,

返回的不再是/data/data/package/files

而是 /data/user/0/package/files 了,如果有多用户,

用户0只能访问/data/user/0/下的数据,用户1只能访问/data/user/1/下的数据,大概这个意思

正常/data/data/package/ 路径下的文件夹大概如下图所示

228d302ea758

1111.png

'app_'开头的,都是app或者第三方sdk新建的

ContextImpl 源码 有如下方法

@Override

public File getDir(String name, int mode) {

checkMode(mode);

name = "app_" + name;

File file = makeFilename(getDataDir(), name);

if (!file.exists()) {

file.mkdir();

setFilePermissionsFromMode(file.getPath(), mode,

FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH);

}

return file;

}

cache > getCacheDir()

databaes > getDatabasePath(String name)

files > getFilesDir()

shared_prefs > getSharedPreferencesPath(String name)

这样每一个目录都能查到,遍历递归删除即可。

当然可以简单粗暴点直接调用

ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

boolean flag = activityManager.clearApplicationUserData();

缺点是会直接退出应用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值