Android Nougat 7.1.2环境下Java实现终端命令发送技术求助
How to Run Root Commands from Android Java Code (Disable SystemUI)
Hey there! Since you're a Java/Android beginner working with a rooted Nougat 7.1.2 device, let's break down exactly how to execute that su pm disable com.android.systemui command when a user taps a button.
Core Idea
On rooted Android devices, you can use Java's Runtime class to spawn a root shell, then write your desired command into that shell's input stream. This lets you run terminal commands directly from your app.
Complete Code Example
Here's a ready-to-use snippet you can add to your Activity (make sure you have a button with the ID btn_disable_systemui in your layout):
import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button disableBtn = findViewById(R.id.btn_disable_systemui); disableBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { executeRootCommand("pm disable com.android.systemui"); } }); } // Helper method to run root commands private void executeRootCommand(String command) { Process process = null; BufferedReader reader = null; try { // Start a root shell process process = Runtime.getRuntime().exec("su"); // Write the command to the shell's input stream process.getOutputStream().write((command + "\n").getBytes()); // Send an "exit" command to close the shell process.getOutputStream().write("exit\n".getBytes()); process.getOutputStream().flush(); // Wait for the command to finish and check exit code int exitCode = process.waitFor(); if (exitCode == 0) { Toast.makeText(this, "SystemUI disabled successfully!", Toast.LENGTH_SHORT).show(); } else { // Read error output to debug reader = new BufferedReader(new InputStreamReader(process.getErrorStream())); StringBuilder error = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { error.append(line).append("\n"); } Toast.makeText(this, "Failed to disable SystemUI:\n" + error, Toast.LENGTH_LONG).show(); } } catch (IOException | InterruptedException e) { e.printStackTrace(); Toast.makeText(this, "Error executing root command: " + e.getMessage(), Toast.LENGTH_LONG).show(); } finally { // Clean up resources try { if (reader != null) reader.close(); if (process != null) process.destroy(); } catch (IOException e) { e.printStackTrace(); } } } }
Critical Notes for You
- Recovery Plan: Disabling SystemUI will remove the navigation bar and status bar—you won't be able to navigate back or access settings normally! Make sure you have a way to re-enable it, like connecting your device to a computer and running
adb shell su pm enable com.android.systemuivia ADB, or having another launcher/app that lets you run commands. - Root Access: Your app needs to be granted root permission when it first runs the
sucommand. Some root managers (like Magisk) will pop up a prompt asking for approval—make sure you allow it. - Android Nougat Specifics: The
pm disablecommand works on Nougat, but keep in mind that some devices might have vendor-specific restrictions. Test on your device first! - Error Handling: The code includes basic error handling and reads error output—this will help you debug if something goes wrong (like if root is not properly granted).
内容的提问来源于stack exchange,提问作者redYmir




