Connect Android socket wifi to PC to implement simple PPT controller (source code)

Source: Internet
Author: User

The above is a simple operation on the mobile phone end

Through this article, I just want to briefly introduce Android socket programming.

Upstream ServerCode:

Package Nate. PPT. control; 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; public class pptserver {private final static int right = 1; private final static int left = 2; private final static int shiftf5 = 0; private final stat IC int ESC = 3; Private Static int key; // note that the object Private Static objectinputstream fromclient of the input and output stream is used here; Private Static objectoutputstream fromserver; public static void main (string [] ARGs) throws ioexception, classnotfoundexception, awtexception, interruptedexception {serversocket ssocket = new serversocket (2011); system. out. println ("waiting a connection from the client"); Robot robot = new robot (); socket so Ck = ssocket. accept (); system. out. println ("Recv a connection"); fromclient = new objectinputstream (sock. getinputstream (); fromserver = new objectoutputstream (sock. getoutputstream (); do {choices choice = (choices) fromclient. readobject (); system. out. println ("the flag is" + choice. getkey (); Key = choice. getkey (); Switch (key) {Case shiftf5: robot. keypress (keyevent. vk_shift); thread. sleep (20); robot. keypress (K Eyevent. vk_f5); thread. sleep (10); robot. keyrelease (keyevent. vk_f5); robot. keyrelease (keyevent. vk_shift); thread. sleep (10); break; case left: robot. keypress (keyevent. vk_left); thread. sleep (10); robot. keyrelease (keyevent. vk_left); thread. sleep (10); break; case right: robot. keypress (keyevent. vk_right); thread. sleep (10); robot. keyrelease (keyevent. vk_right); thread. sleep (10); break; Case ESC: robot. keypress (keyevent. vk_e SCAPE); thread. Sleep (10); robot. keypress (keyevent. vk_escape); thread. Sleep (10); break; default: break;} while (key! =-1); system. out. println ("exit the app"); fromclient. close (); fromserver. close (); sock. close (); ssocket. close ();}}

In this example, pay attention to the input and output stream objects used. It is clear about this Java object. In this example, the robot in Java is used to simulate the buttons, that is, the shortcut keys in the PPT to control the PPT. Of course, we all know that the following conditions are first required for transmitting objects using objectinputstream and objectoutputstream. That is, the class to which the transmitted object belongs. This class must implement the serializable interface! At the same time, you must have the same type on the Android mobile client !!! Copy this class to the beginning. This is a Java knowledge.

 
Package Nate. PPT. control; import Java. io. serializable; public class choices implements serializable {private int key; public choices (INT key) {super (); this. key = key;} public int getkey () {return key;} public void setkey (INT key) {This. key = Key ;}}

The preceding class contains the transmitted information and data content.

Let's take a look at the client code and deploy it on the Android mobile phone:

Package Nate. PPT. control; import Java. io. ioexception; import Java. io. objectinputstream; import Java. io. objectoutputstream; import java.net. inetaddress; import java.net. socket; import java.net. unknownhostexception; import android. app. activity; import android. app. alertdialog; import android. content. dialoginterface; import android. content. intent; import android. OS. bundle; import android. view. keyevent; import android. view. view; import android. widget. button; public class pptclient extends activity {private button start; private button escape; private button forward; private button back; private socket sock; private objectoutputstream fromclient; private objectinputstream fromserver; private Final Static int right = 1; private final static int left = 2; private final static int shiftf5 = 0; private final static int ESC = 3; /** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); try {// sock = new socket (inetaddress. getbyname ("125.71.69.199"), 2011); sock = new socket (inetaddress. getbyname ("125.70.223.165"), 2011); fromclient = new objectoutputstream (sock. getoutputstream (); fromserver = new objectinputstream (sock. getinputstream ();} catch (unknownhostexception E1) {e1.printstacktrace ();} catch (ioexception E1) {e1.printstacktrace ();} start = (button) This. findviewbyid (R. id. start); escape = (button) This. findviewbyid (R. id. escape); Forward = (button) This. findviewbyid (R. id. froward); Back = (button) This. findviewbyid (R. id. back); start. setonclicklistener (New button. onclicklistener () {@ overridepublic void onclick (view v) {choices choice = new choices (shiftf5); try {fromclient. writeobject (choice); system. out. println ("Send the start SHIFT + F5");} catch (ioexception e) {e. printstacktrace () ;}}); escape. setonclicklistener (New button. onclicklistener () {@ overridepublic void onclick (view arg0) {choices choice = new choices (ESC); try {fromclient. writeobject (choice); system. out. println ("Send the escape");} catch (ioexception e) {e. printstacktrace () ;}}); forward. setonclicklistener (New button. onclicklistener () {@ overridepublic void onclick (view v) {choices choice = new choices (right); try {fromclient. writeobject (choice); system. out. println ("send the right (the next)");} catch (ioexception e) {e. printstacktrace () ;}}); back. setonclicklistener (New button. onclicklistener () {@ overridepublic void onclick (view v) {choices choice = new choices (left); try {fromclient. writeobject (choice); system. out. println ("Send the left (the last)");} catch (ioexception e) {e. printstacktrace () ;}});}/*** listener back key * @ Param keycode * @ Param event * @ return */Public Boolean onkeydown (INT keycode, keyevent event) {If (event. getkeycode () = keyevent. keycode_back) {alertdialog. builder = new alertdialog. builder (this); builder. settitle ("Exit app"); builder. setmessage ("You will exit the app... "); // builder. seticon (R. drawable. stat_sys_warning); builder. setpositivebutton ("OK", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {intent startmain = new intent (intent. action_main); startmain. addcategory (intent. category_home); startmain. setflags (intent. flag_activity_new_task); startactivity (startmain); system. exit (0) ;}}); builder. setnegativebutton ("cancel", new dialoginterface. onclicklistener () {@ overridepublic void onclick (dialoginterface dialog, int which) {}}); builder. show ();} return Super. onkeydown (keycode, event );}}

 

The code is still very simple. I will not talk about it here. It is emphasized that the client side has the choices class above in addition to the activity class !!! Same as the server type! At the same time, do not forget to add it to the android manifest. xml file.

 
<Uses-Permission Android: Name = "android. Permission. access_network_state"/> <uses-Permission Android: Name = "android. Permission. Internet"/>

User permission !!! Don't forget to add...
Of course, there are still many improvements to the Code. For example, if you want to solve the problem that the press may delay the PPT, but you do not know whether the press is actually performed, we can add a vibrating effect on the buttons on the mobile phone end, so that we can accurately know whether we press the button on the mobile phone. This should not be difficult! But this articleArticleThis section briefly introduces the connection between Android socket programming and PC.

If a friend needs the source code of the entire project, leave an email below and I will send it to you!


As you need a lot of netizens, I have already put resources here, at http://download.csdn.net/detail/natepan/3849822. You can freely download them. Thank you.

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.