Because often open notebook work, tired of sitting on the bed to play mobile phone, but at night to sleep and don't want to get out of bed to shut down the computer, want to do an Android software to control the PC side shutdown and restart. To achieve this, you have to communicate between your Android phone and your PC, so you can do it with socket programming.
==================================The use of SHUTDOWN commands ========================================
First you need to know about the usage of the shutdown command.
Select Win+r, open the Command Line window, enter shutdown/? Press ENTER to enter the key,
You can see the usage of some shutdown:
Shutdown-l logoff. This cannot be used with the/m or/d option.
Shutdown-s shut down the computer.
Shutdown-r completely shut down and restart the computer.
Shutdown-a Abort system shutdown.
Shutdown-p shuts down the local computer without a timeout or warning.
Shutdown-h Hibernate the local computer.
Shutdown-t xxx will be turned off before the timeout period is set to xxx seconds.
Shutdown-c "comment" for a comment about the cause of the restart or shutdown.
SHUTDOWN-F forces the running application to close without warning the user beforehand.
shutdown-d [P|u:]xx:yy provides reasons for restarting or shutting down.
Here are some of the most common features of shutdown, which are timed shutdown, restart, and cancel shutdown.
Timed shutdown, i.e. shutdown-s-T "fill time here" such as: shutdown-s-T 60
The time here is counted in seconds, which is one minute after the shutdown.
Timed shutdown and restart, i.e. Shutdown-r-T "fill time here" such as: Shutdown-r-T 60
The time here is calculated in seconds, that is, a minute after the shutdown R and then restart.
We can use Shutdown-a to cancel the scheduled shutdown task, but in the first second of shutting down the machine
================================ implementation of PC server Side ===========================================
PC-side server code is very simple, is to listen to port 30000, and then get Android sent instructions, using the Runtime.getruntime (). EXEC (String command) method to perform the shutdown command described above, To achieve the ability to control shutdown, restart, or cancel shutdown of PC-side.
The port number must be idle, the port number range from 0 to 65535, like the network Port 80,ftp Port 21,mysql The default port 3306, I use the 30000 port, generally will not be occupied.
The code is as follows:
Package Com.oyp.shutdown;import Java.io.datainputstream;import Java.io.dataoutputstream;import java.io.IOException ; Import Java.net.serversocket;import Java.net.socket;public class Shutdownserver {static ServerSocket ServerSocket = null;//service socketstatic DataInputStream datainput = null;//input stream static DataOutputStream DataOutput = null;//output stream public Stati c void Main (string[] args) {try {//Listen on 30000 port serversocket = new ServerSocket (30000); System.out.println ("Shutdownserver is listening port 30000 ....."); while (true) {//Gets the client socket socket Clientsocket = serversocket.accept (); String send_msg = ""; try {//Get input stream, read data from client Datainput = new DataInputStream (Clientsocket.getinputstream ()); String msg = Datainput.readutf (); SYSTEM.OUT.PRINTLN (msg);//Determine input, perform corresponding operation dataoutput = new DataOutputStream (Clientsocket.getoutputstream ()); Shutdown ". Equals (msg)) {shutdown ();//Send message back to Android send_msg =" Shut down after 60 Seconds ";} else if ("reboot". Equals (msg)) {reboot (); send_msg = "Restarts after 60 Seconds";} else if ("Cancel". Equals (msg)) {Cancel (); send_msg = "Cancel shutdown or restart";}} catch (Exception e) {} finally {try {if (dataoutput! = null) {Dataoutput.writeutf (send_msg);d ataoutput.close ();} Datainput.close ();//close connection clientsocket.close ();} catch (IOException e) {e.printstacktrace ();}}}} catch (Exception e) {e.printstacktrace ();}} Shutdown private static void shutdown () throws IOException {Runtime.getruntime (). EXEC ("shutdown-s-T 60"); System.out.println ("Shutdown, Seconds later");} Restart private static void reboot () throws IOException {Runtime.getruntime (). EXEC ("shutdown-r-T 60"); System.out.println ("Reboot, Seconds later");} Cancel shutdown or restart private static void Cancel () throws IOException {Runtime.getruntime (). EXEC ("shutdown-a"); System.out.println ("Cancel shutdown or restart");}}
After running, the effect is as follows:
1, has been listening to port 30000
2. When the Android client sends a shutdown request
3. when the Android client sends a cancel shutdown request
4. when the Android client sends a restart request
==================================================================================================
Ouyangpeng welcome reprint, sharing with people is the source of progress!
Reprint please keep the original address : Http://blog.csdn.net/ouyang_peng
==================================================================================================
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
My Android Advanced tour------>android realizes the ability to control PC-side shutdown and restart with Android phone (a) PC server side