Start searching from the Internet and use the action method, but it has never been successful.
Intent intent = new Intent ();
Intent. setAction (Intent. ACTION_SHUTDOWN );
SendBroadcast (intent );
Add Permissions
<Uses-permission android: name = "android. permission. SHUTDOWN" tools: ignore = "ProtectedPermissions"/>
If a successful employee wishes to send a message, thank you.
Here I will introduce my methods.
1. The power service enables shutdown.
Framework/base/services/java/com/android/server/power/PowerManagerService. java
/**
* Shuts down the device.
*
* @ Param confirm If true, shows a shutdown confirmation dialog.
* @ Param wait If true, this call waits for the shutdown to complete and does not return.
*/
@ Override // Binder call
Public void shutdown (boolean confirm, boolean wait ){
MContext. enforceCallingOrSelfPermission (android. Manifest. permission. REBOOT, null );
Final long ident = Binder. clearCallingIdentity ();
Try {
ShutdownOrRebootInternal (true, confirm, null, wait );
} Finally {
Binder. restoreCallingIdentity (ident );
}
}
2. PowerManager provides reboot and Other interfaces, but does not provide shutdown interfaces.
If it is restarted, the implementation is simple:
PowerManager pm = (PowerManager) this. getSystemService (Context. POWER_SERVICE );
Pm. reboot ();
However, shutdown is not implemented. PowerManager uses IPowerManager to call the Power service interface.
IPowerManager is a class automatically generated by AIDL files to facilitate remote communication. IPowerManage. aidl file directory framework/base/core/java/android/OS/IPowerManage. aidl
3. IPowerManager implements the shutdown interface. Here, you only need to obtain the IBinder of the Power service and call the shutdown method through reflection to implement the shutdown function.
ServiceManager manages the system's service programs. It stores the IBinder of all services and obtains the IBinder of the service through the service name.
The ServiceManager class is also HIDE and needs reflection for calling.
Code implementation:
[Java] view plaincopyprint?
Try {
// Obtain the ServiceManager class
Class <?> ServiceManager = Class
. ForName ("android. OS. ServiceManager ");
// Obtain the getService method of ServiceManager
Method getService = ServiceManager. getMethod ("getService", java. lang. String. class );
// Call getService to obtain RemoteService
Object oRemoteService = getService. invoke (null, Context. POWER_SERVICE );
// Obtain the IPowerManager. Stub class
Class <?> CStub = Class
. ForName ("android. OS. IPowerManager $ Stub ");
// Obtain the asInterface Method
Method asInterface = cStub. getMethod ("asInterface", android. OS. IBinder. class );
// Call the asInterface method to obtain the IPowerManager object
Object oIPowerManager = asInterface. invoke (null, oRemoteService );
// Obtain the shutdown () method
Method shutdown = oIPowerManager. getClass (). getMethod ("shutdown", boolean. class, boolean. class );
// Call the shutdown () method
Shutdown. invoke (oIPowerManager, false, true );
} Catch (Exception e ){
Log. e (TAG, e. toString (), e );
}
Try {
// Obtain the ServiceManager class
Class <?> ServiceManager = Class
. ForName ("android. OS. ServiceManager ");
// Obtain the getService method of ServiceManager
Method getService = ServiceManager. getMethod ("getService", java. lang. String. class );
// Call getService to obtain RemoteService
Object oRemoteService = getService. invoke (null, Context. POWER_SERVICE );
// Obtain the IPowerManager. Stub class
Class <?> CStub = Class
. ForName ("android. OS. IPowerManager $ Stub ");
// Obtain the asInterface Method
Method asInterface = cStub. getMethod ("asInterface", android. OS. IBinder. class );
// Call the asInterface method to obtain the IPowerManager object
Object oIPowerManager = asInterface. invoke (null, oRemoteService );
// Obtain the shutdown () method
Method shutdown = oIPowerManager. getClass (). getMethod ("shutdown", boolean. class, boolean. class );
// Call the shutdown () method
Shutdown. invoke (oIPowerManager, false, true );
} Catch (Exception e ){
Log. e (TAG, e. toString (), e );
}