Multiple Ways to Realize Shutdown in Android

  • 2021-12-13 09:19:30
  • OfStack

Directory Mode 1 Mode 2 Mode 3 Authority:
Mode 4, Mode 5, Mode 6

If you want to shut down in the code, you need apk to have system permissions, and you should add it in manifest file android:sharedUserId=“android.uid.system” , but also with the system signature.

Mode 1

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


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

Mode 2

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


try {
    PowerManager pManager = (PowerManager) VfiServiceApp.getContext().getSystemService(Context.POWER_SERVICE);
    if (pManager != null) {
        Method method = pManager.getClass().getMethod("shutdown", boolean.class, String.class, boolean.class);
        method.invoke(pManager, false, null, false);
    }
} catch (Exception e) {
    e.printStackTrace();
}

Mode 3

Send a broadcast

Broadcast

Intent.ACTION_REQUEST_SHUTDOWN Shutdown broadcast
Intent.ACTION_REBOOT Restart the broadcast

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


public static final String ACTION_REQUEST_SHUTDOWN = "android.intent.action.ACTION_REQUEST_SHUTDOWN"
public static final String ACTION_REBOOT = "android.intent.action.REBOOT"

Authority:

Adding code in AndroidMenifest. xml

android:sharedUserId="android.uid.system" Promotion to system permissions
<uses-permission android:name="android.permission.SHUTDOWN" /> Add Shutdown Permission

You need to compile the project in the source code, so you need to add the Android. mk file under 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)   

Mode 4

Start the system service through init. rc to run the sh file

After the Android file system is started,/init is called first. init file will parse init. rc and init. xxx. rc and then execute it. init. rc will do some simple initialization operations in the system initialization process, and can use init process to parse the shutdown or restart script added by itself.

Write a shutdown or restart script sh file


#!/system/bin/sh
reboot

#!/system/bin/sh
reboot -p    # Or shutdown

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 init. rc file and add the following at the end of the file:


 service system_shutdown /system/bin/system_shutdown.sh  # No. 1 1 In step sh Filename   
        oneshot  # Start only 1 Times 
        disabled # Disable the service, does not boot self-startup, but can start manually in the application   
service 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 guide the folder to the system path, and then compile the source code.
You can call the system service to restart or shut down in code


SystemProperties.set("ctl.start", "system_shutdown"); //system_shutdown Yes sh The file name of the script 
SystemProperties.set("ctl.start", "system_reboot");  

Mode 5

Start the system service through init. rc to run the sh file (also the most commonly used method)


 //Runtime Execute linux - shell  
case R.id.shutdown_btn3:  
    try{  
        Log.v(TAG, "root Runtime->shutdown");  
        //Process proc =Runtime.getRuntime().exec(new String[]{"su","-c","shutdown"});  // Shut down   
        Process proc =Runtime.getRuntime().exec(new String[]{"su","-c","reboot -p"});  // Shut down   
        proc.waitFor();  
    }catch(Exception e){  
        e.printStackTrace();  
    }  
    break;  
case R.id.reboot_btn3:  
    try {   
        Log.v(TAG, "root Runtime->reboot");  
        Process proc =Runtime.getRuntime().exec(new String[]{"su","-c","reboot "});  // Shut down   
        proc.waitFor();  
    }catch (Exception ex){  
        ex.printStackTrace();  
    }  
    break;   

The premise is that there are reboot and shutdown files in android system system/bin directory, which are available for most models of devices.
The device needs to obtain root permission.

Mode 6

PowerManager provides the reboot interface


try {
    PowerManager pManager = (PowerManager) VfiServiceApp.getContext().getSystemService(Context.POWER_SERVICE);
    if (pManager != null) {
        Method method = pManager.getClass().getMethod("shutdown", boolean.class, String.class, boolean.class);
        method.invoke(pManager, false, null, false);
    }
} catch (Exception e) {
    e.printStackTrace();
}
0

The above is the Android shutdown implementation of a variety of ways of the details, more about Android shutdown information please pay attention to other related articles on this site!


Related articles: