Vai*_*wal 0 java variables android class
我有以下代码用于执行root命令:
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;
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
variable "output" is accessed from within inner class. Needs to be declared final.
Run Code Online (Sandbox Code Playgroud)
我不知道如何分配"内部类"的输出.
错误消息说明了一切:您只能final从内部类中访问变量.
一个快速的解决方案是定义:
final String output[] = new String[1]; //init string output
Run Code Online (Sandbox Code Playgroud)
在内部阶级:
output[0] = line; // No ERROR :-)
Run Code Online (Sandbox Code Playgroud)
然后:
return output[0];
Run Code Online (Sandbox Code Playgroud)
这是因为数组本身是最终的,但是数组的内容仍然可以改变(final如果你问我,在Java中有点奇怪的定义;最终并不意味着不可变).
| 归档时间: |
|
| 查看次数: |
1944 次 |
| 最近记录: |