J'ai un exécutable compilé qui est censé se copier du dossier res, et dans le dossier/data/data/nom-paquet /, et modifier les permissions, puis exécuter. Chaque étape se termine tout le chemin jusqu'à la fin. Le flux de sortie semble écrire, etc. Sauf quand je vais vérifier le système de fichiers, rien n'a été fait. J'ai d'abord essayé avec 'sh' puis avec 'su' (j'ai un Cyanogen rom enraciné).Écrire une application Android qui exécute des commandes Linux
Voici le code:
public class UnexecutableActivity extends Activity {
String executablePath;
TextView outputView;
private UnexecutableTask mUnexecutableTask;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
executablePath = getIntent().getData().getPath();
System.out.println(executablePath);
setContentView(R.layout.main);
outputView = (TextView)findViewById(R.id.outputView);
try {
Process process = Runtime.getRuntime().exec("su");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Runnable runexecutable = new Runnable(){
@Override
public void run() {
mUnexecutableTask = new UnexecutableTask();
mUnexecutableTask.execute("");
}
};
runOnUiThread(runexecutable);
}
private class UnexecutableTask extends AsyncTask<String, String, String> {
public UnexecutableTask() {
super();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
outputView.setText(outputView.getText() + "\n" + executablePath + "converted to ");
}
@Override
protected void onPreExecute() {
super.onPreExecute();
outputView.setText("About to un-executable " + executablePath + " ...");
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
outputView.setText(outputView.getText() + "\n" + values[0]);
}
@Override
protected String doInBackground(String... params) {
String bashEscapedPath = executablePath.replace(" ", "\\ ");
try{
String[] commands;
publishProgress("Loading unexecutable...");
InputStream unexecutableInputStream = getAssets().open("unexecutable");
FileOutputStream fos = new FileOutputStream(getDir("", MODE_WORLD_WRITEABLE) + "/unexecutable");
byte[] tmp = new byte[2048];
int l;
while ((l = unexecutableInputStream.read(tmp)) != -1) {
fos.write(tmp, 0, l);
}
fos.flush();
fos.close();
unexecutableInputStream.close();
publishProgress("Changing file permissions...");
commands = new String[] {"/system/bin/chmod","744", getDir("", MODE_WORLD_WRITEABLE) + "/unexecutable"};
Process process = Runtime.getRuntime().exec("su");
StringBuffer res = new StringBuffer();
DataOutputStream os = new DataOutputStream(process.getOutputStream());
DataInputStream osRes = new DataInputStream(new
BufferedInputStream(process.getInputStream()));
for (String single : commands) {
os.writeBytes(single + "\n");
os.flush();
//publishProgress(String.valueOf(osRes.readByte()));
}
os.writeBytes("exit\n");
os.flush();
process.waitFor();
publishProgress("Performing un-executable...");
commands = new String[] {"/data/data/" + getPackageName() + "/unexecutable", bashEscapedPath};
process = Runtime.getRuntime().exec("su");
res = new StringBuffer();
os = new DataOutputStream(process.getOutputStream());
osRes = new DataInputStream(process.getInputStream());
for (String single : commands) {
os.writeBytes(single + "\n");
os.flush();
}
os.writeBytes("exit\n");
os.flush();
publishProgress("Finishing...");
process.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "Success";
}
}
Si quelqu'un pouvait résoudre ce problème pour moi, (et nous espérons utiliser sh!) Je serais éternellement reconnaissant.
Je ne suis pas si familier avec pastebin - pourriez-vous faire un lien s'il vous plaît. – Martin
pourquoi ne pas simplement mettre le code ici? SO prend en charge la coloration syntaxique et autres. –
quelle version de CM exécutez-vous car chmod n'existe pas dans/system/bin. son/in/system/xbin/- im fonctionnant 5.0.8 –