和机器人编码 Java Excut su 命令和不工作
原标题:android coding java excute su command android not work
i have a device rooted by magisk. i want to make an app to excute "su ls data/data". But when i run app it only returned two packages eg: com.google.android.gms, com.nghiatd.nghiaclicker( this is package name of my app). But when i excute that command on terminal emulator app it returned full package name of apps in data/data folder. i have added read,write,ACCESS_SUPERUSER permission in manifest. magisk granted permission.i have tested on android 10. that funtion returned full package name of all apps. anyone know why android 12 does not return full ? This is funtion:
`
enter code here
public static String executeCommandSU( String command)
{
try {
Process process = new ProcessBuilder().command("su").start();
process.getOutputStream().write((command + "
").getBytes("ASCII"));
//process.getOutputStream().write(("ls data" + "
").getBytes("ASCII"));
process.getOutputStream().flush();
SystemClock.sleep(3000);
StringBuffer sb = new StringBuffer();
BufferedReader bre = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
while (bre.ready()) {
sb.append(bre.readLine());
}
String s = sb.toString();
if (!s.replaceAll(" ", "").equalsIgnoreCase("")) {
Log.e("SystemHelper", "Error with command: " + s);
return s;
}
sb = new StringBuffer();
BufferedReader br = new BufferedReader(
new InputStreamReader(process.getInputStream()));
while (br.ready()) {
sb.append(br.readLine()+"
");
}
s = sb.toString();
if (!s.replaceAll(" ", "").equalsIgnoreCase("")) {
Log.e("`SystemHelper`", "Output from command: " + s);
return s;
}
return s;
} catch (IOException e) {
Log.e("SystemHelper", "Error executing: " + command, e);
}
return "";
}`
`
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermission();
//String s=executeCommand(new String[]{"su","-c","ls data/data"});
String s=executeCommandSU("ls data/data");
Log.i("vvvvv",s);
Toast.makeText(getApplicationContext(),"aaaaaaa: ",Toast.LENGTH_LONG).show();
}
问题回答
You are using ready() incorrectly. Look up the docs to see why. Change:
while (br.ready()) {
sb.append(br.readLine()+"
");
}
to
while (true) {
String l = br.readLine();
if (l == null) // EOF
break;
sb.append(l+"
");
}
Do the same for the error stream.
Also to make su terminate change:
process.getOutputStream().write((command + "
").getBytes("ASCII"));
to:
process.getOutputStream().write((command + "
exit
").getBytes("ASCII"));
Haven t tested it. Hope it works.
edit targetSdk 29 in build.gradle file will solve problem
"Starting from Android 12, security policies have been tightened, and root tools like Magisk have caused Google to disable many features."
相关问题
Spring Properties File
Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...
What do you say of chopping type-4 UUID in this manner
Check this,
List<String> list = new ArrayList<String>();
for (int i = 0; i < 10000; i++) {
String value = (""+UUID.randomUUID().getLeastSignificantBits()).substring(3, ...
combining decorator and state pattern in java - question about OO design
I am in the middle of solving a problem where I think it s best suited for a decorator and a state pattern. The high level setting is something like a sandwich maker and dispenser, where I have a set ...
Unable to execute stored Procedure using Java and JDBC on SQL server
I have been trying to execute a MS SQL Server stored procedure via JDBC today and have been unsuccessful thus far. The stored procedure has 1 input and 1 output parameter. With every combination I ...
Logging a global ID in multiple components
I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications.
[App A] -> [App B] -> [App C]
We set a ...
Java Library Size
If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is:
How will the larger, mostly unused ...
How to get the Array Class for a given Class in Java?
I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this:
Class arrayOfFooClass = java.lang....
SQLite , Derby vs file system
I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database.
How hard is it to migrate ...