I have the following code to use to execute a root command:
public static String sudo(final String cmd, Context ctx) {
String output = null; //init string output
if (RootTools.isRootAvailable()) {
if (RootTools.isAccessGiven()) {
try {
CommandCapture command = new CommandCapture(0, cmd) {
@Override
public void output(int id, String line) {
Log.d("com.vwade79.aokpdelta.Functions.sudo", "cmd:"+cmd+"\noutput:"+line);
if (line.equals("")) {
return;
}
else {
output = line; //ERROR
}
}
};
RootTools.getShell(true).add(command).waitForFinish();
} catch (Exception e) {
Toast.makeText(ctx, "There was an error executing root command : "+cmd, Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
else {
Toast.makeText(ctx, "Root permission isn't given!", Toast.LENGTH_LONG).show();
Intent intent = new Intent(ctx, MainActivity.class);
ctx.startActivity(intent);
}
}
else {
Toast.makeText(ctx, "You're not rooted! Come back when you are!", Toast.LENGTH_LONG).show();
Intent intent = new Intent(ctx, MainActivity.class);
ctx.startActivity(intent);
}
return output;
}
I am getting an error:
variable "output" is accessed from within inner class. Needs to be declared final.
I don't know how to assign the output from the "inner class".
The error message says it all: you can only access final
variables from within an inner class.
A quick solution would be to define:
final String output[] = new String[1]; //init string output
and in the inner class:
output[0] = line; // No ERROR :-)
and then:
return output[0];
This is, because the array itself is final, but the contents of the array can still be changed (a little strange definition of final
in Java, if you ask me; final does not mean immutable).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.