English 中文(简体)
和机器人编码 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 ...

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 ...

热门标签