Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 中級開發 >> Android開發進階之NIO非阻塞包(七)

Android開發進階之NIO非阻塞包(七)

編輯:中級開發

今天我們繼續就Android DDMS源碼一起分析NIO非阻塞通訊方式,android123也會給大家分享下手機和PC互通中的一些技術。在NIO中有關SocketChannel和ByteBuffer的使用細節,可以在今天文章中

   static void read(SocketChannel chan, byte[] data, int length, int timeout)
            throws TimeoutException, IOException {
        ByteBuffer buf = ByteBuffer.wrap(data, 0, length != -1 ? length : data.length); //從字節數組中實例化ByteBuffer
        int numWaits = 0;

        while (buf.position() != buf.limit()) {  //循環接收數據
            int count;

            count = chan.read(buf);
            if (count < 0) {
                    throw new IOException("EOF"); //讀到末尾
            } else if (count == 0) {
                if (timeout != 0 && numWaits * WAIT_TIME > timeout) {
                    throw new TimeoutException();
                }
                 try {
                    Thread.sleep(WAIT_TIME);
                } catch (InterruptedException IE) {
                }
                numWaits++;
            } else {
                numWaits = 0;
            }
        }
    }

    有關SocketChannel的寫操作,就是發送數據代碼如下:

  static void write(SocketChannel chan, byte[] data, int length, int timeout)
            throws TimeoutException, IOException {
        ByteBuffer buf = ByteBuffer.wrap(data, 0, length != -1 ? length : data.length);
        int numWaits = 0;

        while (buf.position() != buf.limit()) {
            int count;

            count = chan.write(buf); //發送數據從ByteBuffer中
            if (count < 0) {
                       throw new IOException("channel EOF");
            } else if (count == 0) {
                             if (timeout != 0 && numWaits * WAIT_TIME > timeout) {
                            throw new TimeoutException();
                }
                 try {
                    Thread.sleep(WAIT_TIME);
                } catch (InterruptedException IE) {
                }
                numWaits++;
            } else {
                numWaits = 0;
            }
        }
    }

   有關ADB如何選擇一個具體的設備,可以使用 setDevice 方法,這樣當電腦中有模擬器或鏈接了多個手機,可以通過設備序列號,選擇需要通訊的設備。

  static void setDevice(SocketChannel adbChan, IDevice device)
            throws TimeoutException, AdbCommandRejectedException, IOException {
        // if the device is not -1, then we first tell adb we're looking to talk
        // to a specific device
        if (device != null) {
            String msg = "host:transport:" + device.getSerialNumber(); // 最後的獲取序列號,android123提示大家在adb命令中是adb get-serialno

            byte[] device_query = formAdbRequest(msg);

            write(adbChan, device_query);

            AdbResponse resp = readAdbResponse(adbChan, false /* readDiagString */);
            if (resp.okay == false) {
                throw new AdbCommandRejectedException(resp.message,
                        true/*errorDuringDeviceSelection*/);
            }
        }
    }

    通過PC控制手機重啟的代碼,當然這裡需要Root權限才能執行

   public static void reboot(String into, InetSocketAddress adbSockAddr,
            Device device) throws TimeoutException, AdbCommandRejectedException, IOException {
        byte[] request;
        if (into == null) {
            request = formAdbRequest("reboot:"); //$NON-NLS-1$
        } else {
            request = formAdbRequest("reboot:" + into); //$NON-NLS-1$
        }

        SocketChannel adbChan = null;
        try {
            adbChan = SocketChannel.open(adbSockAddr);
            adbChan.configureBlocking(false);

            // if the device is not -1, then we first tell adb we're looking to talk
            // to a specific device
            setDevice(adbChan, device);

            write(adbChan, request);
        } finally {
            if (adbChan != null) {
                adbChan.close();
            }
        }
    }

  我們可以看到基本上,每個命令的執行,都是用了單獨SocketChannel通過非阻塞方式執行,這樣大大加強了並發,所以DDMS可以一邊處理Logcat打印,顯示堆信息,處理文件管理等等,有關NIO服務器的內容,android開發網將著重分析MonitorThread.Java這個文件,一起說下NIO的框架。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved