我正在尝试将文件(本例中为FlappyBird2.xml)写入另一个应用程序的数据目录(本例中为/data/data/com.dotgears.flappybird/shared_prefs.xml)。但代码确实没什么。我在onCreate事件中请求另一个活动的超级用户权限。这是我的代码:
Button btncheat = (Button) findViewById(R.id.button1);
btncheat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String path = "/data/data/com.dotgears.flappybird/shared_prefs/";
File file = new File(path);
file.mkdirs();
path += "FlappyBird2.xml";
OutputStream myOutput;
try {
myOutput = new BufferedOutputStream(new FileOutputStream(path,true));
myOutput.write(new String("test").getBytes());
myOutput.flush();
myOutput.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
/编辑:我现在正在尝试这样:
Button btncheat = (Button) findViewById(R.id.button1);
btncheat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String path = "/data/data/com.dotgears.flappybird/shared_prefs/";
String filename = "FlappyBird2.xml";
String string = "test";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
final Runtime runtime = Runtime.getRuntime();
try {
runtime.exec("su");
runtime.exec("mv " + Environment.getDataDirectory().toString() + filename + " " + path + filename);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
但这也没有做任何事情。它成功地在/data/data/my.app/files中创建文件,但mv命令不执行任何操作。我看到它在尝试移动文件之前成功请求了SU,所以这不是问题所在。 Logcat没有说红色,我调试了它。