这篇文章的目的是为大家提供一个Android端控制PC端软件的目的,如控制电脑上的PPT、音乐播放器等软件。在这里我为大家介绍下基本的流程,并在最后附上我自己调试并实现的代码,不足之处请批评指正。
首先,Android遥控PC端,主要的框架是基于客户端/服务器模式,基本上就是android作为客户端,然后PC上运行服务器,两者之间通过Socket连接进行服务器与客户端的数据传输响应,最后由服务端调用相关方法操作PC软件。大致流程如下:
这里需要注意的就是socket连接的时候不要放在主线程中,不然会报错,(android4.0以上),所以我这里使用的是AsyncTask来实现异步操作。第二个注意的地方是连接自己主机的时候还是在命令行里查看下自己的IP地址吧,ipconfig,不要相信什么127.0.0.1或者10.0.2.2的,反正我试了都是不靠谱的,网上几年前的老帖子都是过时了的。
下面是具体代码:主要分为客户端和服务端的:
服务端代码:
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播放热键 private final static String RIGHT = "RightNext"; private final static String LEFT = "LeftForword"; private final static String SHIFTF5 = "ShiftF5"; private final static String ESC = "ESC"; //酷狗音乐播放 private final static String KUGO_START = "AltF5"; private final static String KUGO_NEXT ="AltLeft"; private final static String KUGO_BACK = "AltRight"; private final static String KUGO_PAUSE = "AltF5"; private final static String KUGO_SLIENT = "AltS"; private final static String KUGO_AUDIO_UP = "AltUp"; private final static String KUGO_AUDIO_DOWN = "AltDown"; private final static String KUGO_SHOW_WORDS = "CtrlAltD"; private static int key; private static int num; // 注意这里用的输入输出流的对象 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("等待一个连接"); while (true) { socket = serversocket.accept();//阻塞 System.out.println("获取一个请求"); //连接上之后先构建一个Robot对象 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("请求操作:"+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("请求操作:"+str); robot.keyPress(KeyEvent.VK_RIGHT); robot.keyRelease(KeyEvent.VK_RIGHT); } else if (str.contains(LEFT)) { System.out.println("请求操作:"+str); robot.keyPress(KeyEvent.VK_LEFT); robot.keyRelease(KeyEvent.VK_LEFT); } else if (str.contains(ESC)) { System.out.println("请求操作:"+str); robot.keyPress(KeyEvent.VK_ESCAPE); robot.keyRelease(KeyEvent.VK_ESCAPE); } /** * 这里开始是酷狗的按键控制 */ else if (str.contains(KUGO_START)) { System.out.println("请求操作:酷狗音乐开始播放"); 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("请求操作:酷狗音乐开始播放"); 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("请求操作:酷狗音乐开始播放"); 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("请求操作:静音"); 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("请求操作:增加音量"); 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("请求操作:降低音量"); 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 block e.printStackTrace(); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { serversocket.close(); socket.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}客户端代码:
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"; private final static String NEXT ="AltLeft"; private final static String BACK = "AltRight"; private final static String PAUSE = "AltF5"; private final static String SLIENT = "AltS"; private final static String AUDIO_UP = "AltUp"; private final static String AUDIO_DOWN = "AltDown"; private final static String SHOW_WORDS = "CtrlAltD"; //Java里字符串一样的话String被认为是一个变量 private Socket clientsocket; private final static int port = 9999; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.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_next); 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(100); 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") @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn_start: if ("播放".contains((btn_start.getText().toString().trim()))) { new Music_Play_AsyncTask().execute(START); btn_start.setText("暂停"); btn_start.setBackground(getResources().getDrawable(R.drawable.music_pause)); } else if ("暂停".contains((btn_start.getText().toString().trim()))) { new Music_Play_AsyncTask().execute(PAUSE); btn_start.setBackground(getResources().getDrawable(R.drawable.music_play1)); btn_start.setText("播放"); } 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> { @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub super.onPostExecute(result); Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show(); } @Override protected String doInBackground(String... params) { // TODO Auto-generated method stub String result = params[0]; switch (result) { case START: try { clientsocket = new Socket("172.21.212.2", port); DataOutputStream out = new DataOutputStream(clientsocket.getOutputStream()); String buffer = START; out.write(buffer.getBytes()); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (clientsocket == null) { result = "操作失败:"+result; } result = "开始播放KuGo"; break; case NEXT: try { clientsocket = new Socket("172.21.212.2", port); DataOutputStream 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 = "操作失败:"+result; } result = "下一首"; break; case BACK: try { clientsocket = new Socket("172.21.212.2", port); DataOutputStream out = new DataOutputStream(clientsocket.getOutputStream()); String buffer = BACK; out.write(buffer.getBytes()); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (clientsocket == null) { result = "操作失败:"+result; } result = "上一首"; break; case SLIENT: try { clientsocket = new Socket("172.21.212.2", port); DataOutputStream out = new DataOutputStream(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); DataOutputStream 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 = "操作失败:"+result; } result = "音量增加"; break; case AUDIO_DOWN: try { clientsocket = new Socket("172.21.212.2", port); DataOutputStream out = new DataOutputStream(clientsocket.getOutputStream()); String buffer = AUDIO_DOWN; out.write(buffer.getBytes()); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (clientsocket == null) { result = "操作失败:"+result; } result = "音量降低"; break; case SHOW_WORDS: try { clientsocket = new Socket("172.21.212.2", port); DataOutputStream out = new DataOutputStream(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; } }}
需要声明的是服务端的代码包含了PPT的控制和音乐播放器的控制,而客户端的代码我只给了音乐播放器端的设置,PPT的雷同就不贴了,具体代码都有注释,大家就自己理解吧。程序测试有效,我已经在家舒服的远距离遥控了。
版权声明:本文为博主原创文章,未经博主允许不得转载。