我需要在应用中执行setprop
命令。
目前我正在尝试将其作为shell命令运行:
public class MainActivity extends AppCompatActivity {
private static final String TAG = "test:MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
exec("setprop service.adb.tcp.port 5555");
}
private void exec(String cmd) {
Process proc = null;
BufferedReader successResult = null;
BufferedReader errorResult = null;
Runtime runtime = Runtime.getRuntime();
Log.d(TAG, "exec command: " + cmd);
try {
proc = runtime.exec(cmd);
successResult = new BufferedReader(new InputStreamReader(proc.getInputStream()));
errorResult = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String s;
while ((s = successResult.readLine()) != null) {
Log.d(TAG, "exec command: " + s);
}
while ((s = errorResult.readLine()) != null) {
Log.d(TAG, "exec command: " + s);
}
} catch (IOException e) {
Log.e(TAG, "Error running shell command:", e);
} finally {
try {
if (successResult != null) {
successResult.close();
}
if (errorResult != null) {
errorResult.close();
}
} catch (IOException e) {
Log.e(TAG, "Error running shell command:", e);
}
if (proc != null) {
proc.destroy();
}
}
}
}
这给我带来了:
执行命令:setprop service.adb.tcp.port 5555
执行命令:setprop:无法将属性'service.adb.tcp.port'设置为'5555'
如何从应用程序中设置系统属性?
编辑:抱歉,我应该补充说明我的设备已经root并且这个应用程序是一个系统应用程序。
sharedUserId="android.uid.shell"
吗? - Matt