美文网首页android学习笔记
android 6.0 /data/data/packagena

android 6.0 /data/data/packagena

作者: heheworld | 来源:发表于2019-09-27 11:50 被阅读0次

最近遇到一个问题,需要清空应用缓存以及数据的时候发现不能清理彻底。
我是调用了context.getCacheDir() 以及 context.getFilesDir() 后遍历删除的。
发现只能删掉app创建的文件,而第三方sdk建立的文件仍然在那里。
通过getFilesDir()获取到路径后,再getParent获取到data/data/packagename 路径再去做遍历递归删除。

  1. 在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.
 * <p>
 * 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.
 * <p>
 * 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/ 路径下的文件夹大概如下图所示

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();

缺点是会直接退出应用。

相关文章

网友评论

    本文标题:android 6.0 /data/data/packagena

    本文链接:https://www.haomeiwen.com/subject/mejeyctx.html