Remote PC control for Android development

Source: Internet
Author: User
<span id="Label3"></p><p><p>The purpose of this article is to provide you with the purpose of controlling pc-side software for android, such as controlling ppt on your computer, music player and other Software. Here I introduce the basic process for you, and at the end of the attached to my own debugging and implementation of the code, the shortcomings please criticize.</p></p><p><p>First of all, Android remote PC side, The main framework is based on the Client/server mode, basically is android as a client, and then run the server on the pc, between the two through the socket connection between the server and the Client's data transmission response, finally, The service side calls the relevant method to operate the PC Software. The approximate process is as follows:</p></p><p style="text-align:center"><p style="text-align:center"><br></p></p><p style="text-align:left"><p style="text-align:left">It is important to note that the socket connection should not be placed in the main thread, or will be error, (android4.0 above), so I use the asynctask here to achieve asynchronous Operation. The second note is to connect their own host or in the command line to see their IP address it, ipconfig, do not believe what 127.0.0.1 or 10.0.2.2, anyway, I tried is not reliable, online a few years ago old posts are Obsolete.</p></p><p style="text-align:left"><p style="text-align:left">Here is the specific code: mainly divided into the client and the Server:</p></p><p style="text-align:left"><p style="text-align:left">Service-side code:</p></p><p style="text-align:left"><p style="text-align:left"></p></p><pre name="code" class="java">Package Com.sjm.robot;import Java.awt.awtexception;import Java.awt.robot;import java.awt.event.keyevent;import Java.io.ioexception;import Java.io.objectinputstream;import Java.io.objectoutputstream;import Java.net.serversocket;import Java.net.socket;import java.io.DataInputStream; public class Pptserver {//ppt play Hotkey private final static String right = "rightnext"; Private final static String left = "leftforword"; Private final static String SHIFTF5 = "ShiftF5"; Private final static String ESC = "esc"; Cool Dog Music Play private final static string Kugo_start = "AltF5";p rivate final static string kugo_next = "altleft";p rivate fina L static string kugo_back = "altright";p rivate final static string kugo_pause = "AltF5";p rivate final static string kugo_s lient = "alts";p rivate final static string kugo_audio_up = "altup";p rivate final static string kugo_audio_down = "altdown" ;p rivate final static String kugo_show_words = "ctrlaltd"; private static int key; private static int NuM Note that the object used here for the input and output stream is private static DataInputStream fromclient; private static ServerSocket serversocket; private static Socket socket; public static void main (string[] Args) {new serverthread (). start (); } public static class Serverthread extends Thread{@Override public void run () {//TODO auto-generated method Stub Super.run (); Try {serversocket = new ServerSocket (9999); System.out.println ("wait for a connection"), while (true) {socket = serversocket.accept ();//block System.out.println ("get a request");// Build a Robot object after the connection first try {Robot Robot = new Robot (); fromclient =new java.io.DataInputStream (socket.getinputstream ()); byte [] buffer = new Byte[512];fromclient.read (buffer); String str = new string (buffer), if (str.contains (SHIFTF5)) {System.out.println ("request operation:" +str); robot.keypress ( keyevent.vk_shift); robot.keypress (keyevent.vk_f5); robot.keyrelease (keyevent.vk_f5); robot.keyRelease ( keyevent.vk_shift);} else if (str.contains (right)) {System.out.println ("request operation:" +str); robot.keypress (keyeVent. vk_right); Robot.keyrelease (keyevent.vk_right);} else if (str.contains (left)) {System.out.println ("request operation:" +str); robot.keypress (keyevent.vk_left); robot.keyrelease ( keyevent.vk_left);} else if (str.contains (ESC)) {System.out.println ("request operation:" +str); robot.keypress (keyevent.vk_escape); robot.keyrelease ( keyevent.vk_escape);} /** * Here starts with a cool dog button control */else if (str.contains (kugo_start)) {System.out.println ("request action: Cool dog music starts playing"); robot.keypress ( keyevent.vk_alt); robot.keypress (keyevent.vk_f5); robot.keyrelease (keyevent.vk_f5); robot.keyrelease (KeyEvent.VK_ ALT);} else if (str.contains (kugo_next)) {System.out.println ("request action: Cool dog music starts playing"); robot.keypress (keyevent.vk_alt); Robot.keypress (keyevent.vk_right); robot.keyrelease (keyevent.vk_right); robot.keyrelease (KeyEvent.VK_ALT);} else if (str.contains (kugo_back)) {System.out.println ("request action: Cool dog music starts playing"); robot.keypress (keyevent.vk_alt); Robot.keypress (keyevent.vk_left); robot.keyrelease (keyevent.vk_left); robot.keyrelease (KeyEvent.VK_ALT);} else if (str.contains (kugo_slient)) {SYSTEM.OUt.println ("request operation: mute"); robot.keypress (keyevent.vk_alt); robot.keypress (keyevent.vk_s); robot.keyrelease ( keyevent.vk_s); Robot.keyrelease (keyevent.vk_alt);} else if (str.contains (kugo_audio_up)) {System.out.println ("request operation: increase volume"); robot.keypress (keyevent.vk_alt); Robot.keypress (keyevent.vk_up); robot.keyrelease (keyevent.vk_up); robot.keyrelease (keyevent.vk_alt);} else if (str.contains (KUGO_AUDIO_DOWN)) {System.out.println ("request operation: volume reduction"); robot.keypress (keyevent.vk_alt); Robot.keypress (keyevent.vk_down); robot.keyrelease (keyevent.vk_down); robot.keyrelease (KeyEvent.VK_ALT);} else if (str.contains (kugo_show_words)) {robot.keypress (keyevent.vk_control); robot.keypress (keyevent.vk_alt); Robot.keypress (keyevent.vk_d); robot.keyrelease (keyevent.vk_d); robot.keyrelease (keyevent.vk_alt); Robot.keyrelease (keyevent.vk_control);}} Catch (awtexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} Catch (ioexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} Try {serversocket.cloSe (); Socket.close (); } catch (ioexception E) {//TODO auto-generated catch block E.printstacktrace (); } } }}</pre>Client Code:<p><p></p></p><p style="text-align:left"><p style="text-align:left"></p></p><pre name="code" class="java">Package Com.example.controlpptonpc;import Java.io.dataoutputstream;import Java.io.ioexception;import Java.net.socket;import Java.net.unknownhostexception;import Android.annotation.suppresslint;import Android.app.activity;import Android.os.asynctask;import Android.os.bundle;import Android.view.View;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.imageview;import Android.widget.toast;public class Kugoplay extends Activity implements Onclicklistener{private Button Btn_start,btn_ Next,btn_back;private Button btn_slient,btn_audio_up,btn_audio_down;private static int flag = 0;private final static String START = "AltF5";p rivate final static string NEXT = "altleft";p rivate final static string back = "altright";p rivate f Inal static string PAUSE = "AltF5";p rivate final static string slient = "alts";p rivate final static string audio_up = "Alt Up ";p rivate final static string Audio_down =" Altdown ";p rivate final static string show_words =" Ctrlaltd ";//java Wordstring is considered a variable private Socket clientsocket;private final static int port = 9999; @Overrideprotected void OnCreate ( Bundle Savedinstancestate) {//TODO auto-generated method stubsuper.oncreate (savedinstancestate); Setcontentview ( r.layout.kugo_play_layout); Init (); click_event ();} /** * */public void init () {btn_start = (button) Findviewbyid (r.id.btn_start); btn_next = (button) Findviewbyid (r.id.btn_n ext); btn_back = (button) Findviewbyid (r.id.btn_back); btn_slient = (button) Findviewbyid (r.id.btn_slient); btn_audio_ up = (button) Findviewbyid (r.id.btn_audio_up); btn_audio_up.getbackground (). setalpha (+); btn_audio_down = (button) Findviewbyid (r.id.btn_audio_down); btn_audio_down.getbackground (). setalpha (100);} public void Click_event () {btn_start.setonclicklistener (this), btn_next.setonclicklistener (this); btn_ Back.setonclicklistener (this), btn_slient.setonclicklistener (this), btn_audio_up.setonclicklistener (this); btn_ Audio_down.setonclicklistener (this);} /** * * @param v */@SuppressLint ("newapi") @Overridepublic void OnClick (View v) {//TODO auto-generated method stubswitch (v.getid ()) {case r.id.btn_start:if ("play". cont Ains ((btn_start.gettext (). toString (). trim ())) {new music_play_asynctask (). execute (start); btn_start.settext ("pause" ); btn_start.setbackground (getresources (). getdrawable (r.drawable.music_pause));} else if (pause). contains ((btn_start.gettext (). toString (). trim ()))) {new music_play_asynctask (). execute (pause); btn_ Start.setbackground (getresources (). getdrawable (r.drawable.music_play1)); btn_start.settext ("play");} Break;case r.id.btn_next:new music_play_asynctask (). execute (next); break;case r.id.btn_back:new music_play_ Asynctask (). Execute (back), break;case r.id.btn_slient:if (0 = = Flag) {new music_play_asynctask (). execute (slient); btn_ Slient.setbackground (getresources (). getdrawable (r.drawable.slient)); flag = 1;} else if (1 = = Flag) {new music_play_asynctask (). execute (slient); btn_slient.setbackground (getresources (). getdrawable ( r.drawable.music_slient)); flag = 0;} Break;case R.id.btn_audio_Up:new music_play_asynctask (). Execute (audio_up); break;case r.id.btn_audio_down:new music_play_asynctask (). Execute (audio_down); break;default:break;}} /** * * @author SJM * */private class Music_play_asynctask extends asynctask<string, Void, string>{@Overrideprotect Ed void OnPostExecute (String Result) {//TODO auto-generated method Stubsuper.onpostexecute (result); Toast.maketext (getapplicationcontext (), result, toast.length_short). show (); @Overrideprotected string Doinbackground (string ... params) {//TODO auto-generated method stubstring result = Params[0];s Witch (result) {case Start:try {clientsocket = new Socket ("172.21.212.2", Port);D ataoutputstream out = new Dataoutputstrea M (clientsocket.getoutputstream ()); String buffer = Start;out.write (buffer.getbytes ());} Catch (unknownhostexception e) {e.printstacktrace ();} catch (ioexception e) {e.printstacktrace ();} if (clientsocket = = Null) {result = "operation failed:" +result;} result = "start playing kugo"; break;case next:try {clientsocket = new Socket ("172.21.212.2 ", Port);D Ataoutputstream out = new DataOutputStream (clientsocket.getoutputstream ()); String buffer = Next;out.write (buffer.getbytes ());} Catch (unknownhostexception e) {e.printstacktrace ();} catch (ioexception e) {e.printstacktrace ();} if (clientsocket = = Null) {result = "operation failed:" +result;} result = "next song"; break;case back:try {clientsocket = new Socket ("172.21.212.2", Port);D ataoutputstream out = new dataoutputs Tream (clientsocket.getoutputstream ()); String buffer = Back;out.write (buffer.getbytes ());} Catch (unknownhostexception e) {e.printstacktrace ();} catch (ioexception e) {e.printstacktrace ();} if (clientsocket = = Null) {result = "operation failed:" +result;} result = "previous"; break;case slient:try {clientsocket = new Socket ("172.21.212.2", Port);D ataoutputstream out = new DATAOUTPU TStream (clientsocket.getoutputstream ()); String buffer = Slient;out.write (buffer.getbytes ());} Catch (unknownhostexception e) {e.printstacktrace ();} catch (ioexception e) {e.printstacktrace ();} Break;case Audio_up:try {clientSocket = new Socket ("172.21.212.2", Port);D ataoutputstream out = new DataOutputStream (clientsocket.getoutputstream ()); String buffer = Audio_up;out.write (buffer.getbytes ());} Catch (unknownhostexception e) {e.printstacktrace ();} catch (ioexception e) {e.printstacktrace ();} if (clientsocket = = Null) {result = "operation failed:" +result;} result = "volume increase"; break;case audio_down:try {clientsocket = new Socket ("172.21.212.2", Port);D ataoutputstream out = new Data OutputStream (clientsocket.getoutputstream ()); String buffer = Audio_down;out.write (buffer.getbytes ());} Catch (unknownhostexception e) {e.printstacktrace ();} catch (ioexception e) {e.printstacktrace ();} if (clientsocket = = Null) {result = "operation failed:" +result;} result = "volume down"; break;case show_words:try {clientsocket = new Socket ("172.21.212.2", Port);D ataoutputstream out = new Data OutputStream (clientsocket.getoutputstream ()); String buffer = Show_words;out.write (buffer.getbytes ());} Catch (unknownhostexception e) {e.printstacktrace ();} catch (ioexception e){e.printstacktrace ();} break;default:break;} Return result;}}}</pre><br>Need to declare that the service side of the code contains the PPT control and music player control, and the Client's code I only gave the music player side of the settings, ppt is not affixed to the same, the specific code has comments, we understand it. The program test is effective, I have been at home comfortable remote control over the long distance.<br><br><p><p></p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Remote PC control for Android development</p></p></span>

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.