Android code Shutdown

Source: Internet
Author: User

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 );
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.