SoFunction
Updated on 2025-03-01

Various ways to shut down Android

To shut down in the code, you need to have system permissions. You need to add it in the manifest file.android:sharedUserId=“”, and also need system signature.

The first method

Use the adb shell command directly and call the reboot command to shut down the machine

try {
    ().exec("reboot -p"); //Shut down} catch (IOException e) {
    ();
}

The second way

Call the shutdown method in PowerManage, but this method is a hidden API and can be called through reflection. The code is as follows:

try {
    PowerManager pManager = (PowerManager) ().getSystemService(Context.POWER_SERVICE);
    if (pManager != null) {
        Method method = ().getMethod("shutdown", , , );
        (pManager, false, null, false);
    }
} catch (Exception e) {
    ();
}

The third way

Send a broadcast

Broadcast

Intent.ACTION_REQUEST_SHUTDOWNTurn off the broadcast
Intent.ACTION_REBOOTRestart the broadcast

ACTION_REQUEST and ACTION_REBOOT are two declared string constants. After receiving these two broadcasts, the system will respond to shutdown or restart operations.
The implementation in the source code is as follows:
Declare the code path:/frameworks/base/core/java/android/content/

public static final String ACTION_REQUEST_SHUTDOWN = ".ACTION_REQUEST_SHUTDOWN"
public static final String ACTION_REBOOT = ""

Permissions:

  • Add code to

android:sharedUserId=""Improved to system permissions
<uses-permission android:name="" />Add shutdown permissions

  • The project needs to be compiled in the source code, so you need to add files in the project root directory:
LOCAL_PATH:= $(call my-dir)  
include $(CLEAR_VARS)  
LOCAL_MODULE_TAGS := optional  
LOCAL_SRC_FILES := $(call all-java-files-under, src)  
LOCAL_PACKAGE_NAME := PowerActionDemo  
LOCAL_CERTIFICATE := platform  
include $(BUILD_PACKAGE)   

The fourth method

Run sh file by starting the system service

After the Android file system is started, the init file will be called first, and the init file will be parsed and then executed. Some simple initialization operations will be performed during the system initialization process. You can use the init process to parse the shutdown or restart script you added.

Write a shutdown or restart script sh file

#!/system/bin/sh
reboot
#!/system/bin/sh
reboot -p    #orshutdown

Write mk file

LOCAL_PATH := $(call my-dir)  
include $(CLEAR_VARS)  
LOCAL_PREBUILT_EXECUTABLES := system_shutdown.sh system_reboot.sh  
LOCAL_MODULE_TAGS := optional  
include $(BUILD_MULTI_PREBUILT)  

Modify the file and add it to the end of the file as follows:

 service system_shutdown /system/bin/system_shutdown.sh  #Share file name in step 1        oneshot  #Only start once        disabled #Disable the service, it will not start automatically, but it can be started manually in the applicationservice system_reboot /system/bin/system_reboot.sh  
        oneshot  
        disabled  

Create a new directory, put the above mk file and two sh scripts into the directory, then add the folder sub-guidance system path, and then compile the source code.
You can call the system service in the code to restart or shut down

("", "system_shutdown"); //system_shutdown is the file name of the sh script("", "system_reboot");  

The fifth method

Run sh files by starting the system service (also the most commonly used method)

 //Runtime executes linux-shellcase .shutdown_btn3:  
    try{  
        (TAG, "root Runtime-&gt;shutdown");  
        //Process proc =().exec(new String[]{"su","-c","shutdown"}); //Shutdown        Process proc =().exec(new String[]{"su","-c","reboot -p"});  //Shut down        ();  
    }catch(Exception e){  
        ();  
    }  
    break;  
case .reboot_btn3:  
    try {   
        (TAG, "root Runtime-&gt;reboot");  
        Process proc =().exec(new String[]{"su","-c","reboot "});  //Shut down        ();  
    }catch (Exception ex){  
        ();  
    }  
    break;   

The premise is that the reboot and shutdown files exist in the Android system/bin directory, and most models of devices have them.
The device needs to obtain root permissions.

The sixth way

PowerManager provides reboot interface

PowerManager pManager=(PowerManager) getSystemService(Context.POWER_SERVICE);    
(null);//Restart

The above is the detailed content of various ways to shut down Android. For more information about shutting down Android, please pay attention to my other related articles!