我的手机已经启动了。我正在试着做一个非常简单的程序。程序应该从app/app文件夹中删除文件。我该怎么做呢?我是新手,所以示例代码是有价值的。
发布于 2012-11-21 17:08:12
如果你的手机是根目录的,你可以通过su-provided发出命令,确认su二进制文件已经存在,并且在你的PATH-since中,安卓系统是Linux的一个变体。只需通过Runtime.exec()执行delete命令,超级用户就会处理权限提示。
下面是我在from this question中使用它的一个简单示例
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();发布于 2012-11-21 17:00:51
您可以使用以下方法递归删除文件夹中的所有文件。
private void DeleteRecursive(File fileOrDirectory) {
if (fileOrDirectory.isDirectory())
for (File child : fileOrDirectory.listFiles())
{
child.delete();
DeleteRecursive(child);
}
fileOrDirectory.delete();
}发布于 2013-01-21 04:50:54
在他的github上,Chainfire提供了一个Shell类的sample implementation,您可以使用它以根用户身份执行rm命令。rm命令是用于删除文件(和文件夹)的命令的Linux变体。
代码片段:
if(Shell.SU.available()){
Shell.SU.run("rm /data/app/app.folder.here/fileToDelete.xml"); //Delete command
else{
System.out.println("su not found");或者,如果您确信su二进制文件是可用的,则可以只运行命令(注释行)并跳过检查
来源:How-To SU
https://stackoverflow.com/questions/13489597
复制相似问题