標籤:
這篇文章的目的是為大家提供一個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 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(); } } }}用戶端代碼:
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;@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_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")@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (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>{@Overrideprotected 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];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的雷同就不貼了,具體代碼都有注釋,大家就自己理解吧。程式測試有效,我已經在家舒服的遠距離遙控了。
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
Android開發之遠距離PC控制