【发布时间】:2023-08-03 09:11:02
【问题描述】:
我是新的安卓应用
在我的应用程序的某些部分,我需要删除 /data/data/PACKAGE_NAME/ 中的所有文件和子文件夹,但保留主文件夹
我也有 root 访问权限!和shell推荐
我使用了两种方法,但都删除了包文件夹
方法一:
public static void ClearData(String Dir) {
String sCommand = "rm -rf " + Dir + "*";
Command command = new Command(0,sCommand);
try {
RootTools.getShell(true).add(command);
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
} catch (RootDeniedException e) {
e.printStackTrace();
}
}
方法二:
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// The directory is now empty so delete it
return dir.delete();
}
tnx
编辑:我找到了解决方案:
public static void ClearData(String Dir) {
String sCommand = "rm -rf " + Dir + "/*";
Command command = new Command(0,sCommand);
try {
RootTools.getShell(true).add(command);
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
} catch (RootDeniedException e) {
e.printStackTrace();
}
}
【问题讨论】:
标签: android