旧码整理。
很多安卓手机管理软件的安装目录里都能发现自带的adb.exe,但一般都是经过二次开发的。
这里是实现此类软件功能的一种设计。
一. 原理图
二. 手机端
1. 长连接服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
public class AdbService extends Service { /** 是否还在聊天中 */ public static boolean isCharting = true; /** 聊天 Socket服务端 */ private ServerSocket serverSocket = null; /** 聊天 socket端口 */ private int SERVER_PORT = 10086; @Override public void onCreate() { super.onCreate(); Log.e("ard", "服务onCreate"); new Thread() { public void run() { Log.e("ard", "服务 run"); serverSocket = null; try { serverSocket = new ServerSocket(SERVER_PORT); while (MainActivity.laidEnable) { if (MainActivity.chardEnable) { Socket client = serverSocket.accept(); Log.e("ard", "socket accept"); new Thread(new AdbThread(client)).start(); } } } catch (IOException e) { Log.e("ard", "服务创建socket异常:" + e.toString()); } } }.start(); } @Override public void onDestroy() { super.onDestroy(); // 关闭线程 isCharting = false; // 关闭服务器 try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } @Override public IBinder onBind(Intent intent) { return null; } class AdbThread implements Runnable { private Socket client; public AdbThread(Socket client) { this.client = client; } @Override public void run() { Log.e("ard", "socket 子线程run"); BufferedOutputStream out = null; BufferedInputStream in = null; String msg = ""; try { Log.e("ard", "socket 子线程通讯"); out = new BufferedOutputStream(client.getOutputStream()); in = new BufferedInputStream(client.getInputStream()); while (AdbService.isCharting) { if (!client.isConnected()) { break; } int MAX_BUFFER_BYTES = 2048; byte[] tempbuffer = new byte[MAX_BUFFER_BYTES]; int numReadedBytes = in.read(tempbuffer, 0, tempbuffer.length); msg = new String(tempbuffer, 0, numReadedBytes, "utf-8"); tempbuffer = null; Log.e("ard", "收到消息:" + msg); out.write("what are you doing".getBytes()); out.flush(); } } catch (Exception e) { Log.e("ard", "socket 子线程异常:" + e.toString()); } finally { try { if (null != in) in.close(); if (null != out) out.close(); if (client != null) client.close(); } catch (IOException e) { Log.e("ard", "socket 子线程关闭socke异常:" + e.toString()); } } } } } |
2. 连接请求监听
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
public class AdbReceiver extends BroadcastReceiver { private String Action_StartChart = "ActionStartChart"; private String Action_StopChart = "ActionStopChart"; private Intent serI; public AdbReceiver(Context context) { Log.e("ard", "AdbReceiver 实例化。"); serI = new Intent(context, AdbService.class); } @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); Log.e("ard", "PC的约炮暗号:" + action); if (Action_StartChart.equalsIgnoreCase(action)) { context.startService(serI); Log.e("ard", Thread.currentThread().getName() + "->" + " 聊天服务启动 "); } else if (Action_StopChart.equalsIgnoreCase(action)) { context.stopService(serI); Log.e("ard", Thread.currentThread().getName() + "->" + "聊天服务停止 "); } } } |
3. 启动监听
1 2 3 4 5 6 7 |
AdbReceiver mReceiver = new AdbReceiver(this); // 定义收音机 IntentFilter ifr = new IntentFilter(); ifr.addAction("ActionStartChart"); // 开始聊天服务 ifr.addAction("ActionStopChart"); // 关闭聊天服务 registerReceiver(mReceiver, ifr); |
二. 管理端(windows)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
public static void main(String[] args) throws InterruptedException { try { Runtime.getRuntime().exec("adb shell am broadcast -a ActionStopChart"); Thread.sleep(3000); Runtime.getRuntime().exec("adb forward tcp:12580 tcp:10086"); Thread.sleep(3000); Runtime.getRuntime().exec("adb shell am broadcast -a ActionStartChart"); Thread.sleep(3000); } catch (IOException e) { e.printStackTrace(); } Socket socket = null; try { InetAddress serverAddr = null; serverAddr = InetAddress.getByName("127.0.0.1"); socket = new Socket(serverAddr, 12580); System.out.println("连接Ard"); BufferedOutputStream out = new BufferedOutputStream(socket.getOutputStream()); BufferedInputStream in = new BufferedInputStream(socket.getInputStream()); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); boolean flag = true; while (flag) { System.out.print("请输入聊天内容:"); String me = br.readLine();// 从控制台输入 out.write(me.getBytes("UTF-8")); out.flush(); System.out.println("PC发出消息:" + me); int MAX_BUFFER_BYTES = 4000; String msg = ""; byte[] tempbuffer = new byte[MAX_BUFFER_BYTES]; int numReadedBytes = in.read(tempbuffer, 0, tempbuffer.length); msg = new String(tempbuffer, 0, numReadedBytes, "utf-8"); System.out.println("Ard发来消息:" + msg); System.out.println("============================================="); } } catch (UnknownHostException e) { System.err.println(e.toString()); } catch (Exception e) { System.err.println(e.toString()); } finally { try { if (socket != null) { socket.close(); } } catch (IOException e) { System.err.println(e.toString()); } } } |
end
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/2286.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设