91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

adb forword通信怎么在android移動端與PC端中使用

發布時間:2020-12-03 16:27:40 來源:億速云 閱讀:125 作者:Leah 欄目:移動開發

這篇文章給大家介紹adb forword通信怎么在android移動端與PC端中使用,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

PC端與Android手機端使用adb forword通信

服務器端代碼如下:

import java.io.IOException; 
import java.io.ObjectOutputStream; 
import java.net.Socket; 
import java.net.UnknownHostException; 
import java.util.Scanner; 
public class Server { 
  public static final String TAG = "server"; 
  public static int PC_LOCAL_PORT = 22222; 
  public static int PHONE_PORT = 22222; 
  public static String ADB_PATH = "adb.exe"; 
  /** 
   * @param args 
   */ 
  public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    YingyonghuiHubServer.execAdb(); 
  } 
  public static void execAdb() { 
    // run the adb bridge 
    try { 
      Process p = Runtime.getRuntime().exec( 
          ADB_PATH + " forward tcp:" + PC_LOCAL_PORT + " tcp:" 
              + String.valueOf(PHONE_PORT)); 
      Scanner sc = new Scanner(p.getErrorStream()); 
      // If there is some output, it failed to start adb 
      if (sc.hasNext()) { 
        while (sc.hasNext()) 
          System.out.println(sc.next()); 
        System.err.println("Cannot start the Android debug bridge"); 
        return; 
      } 
      initializeConnection(); 
    } catch (Exception e) { 
      System.err.println(e.toString()); 
    } 
  } 
  static Socket socket; 
  public static void initializeConnection() { 
    // Create socket connection 
    try { 
      socket = new Socket("localhost", PC_LOCAL_PORT); 
      ObjectOutputStream oos = new ObjectOutputStream( 
          socket.getOutputStream()); 
      oos.writeObject("lalala"); 
      oos.close(); 
      socket.close(); 
    } catch (UnknownHostException e) { 
      System.err.println("Socket connection problem (Unknown host)" 
          + e.getStackTrace()); 
      e.printStackTrace(); 
    } catch (IOException e) { 
      System.err.println("Could not initialize I/O on socket"); 
      e.printStackTrace(); 
    } 
  } 
} 

 客戶端代碼如下:

import java.io.IOException; 
import java.io.ObjectInputStream; 
import java.net.ServerSocket; 
import java.net.Socket; 
import android.app.Activity; 
import android.content.Context; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.widget.TextView; 
import android.widget.Toast; 
public class Client extends Activity { 
  public static final String TAG = "client"; 
  public static int PHONE_PORT = 22222; 
  Context mContext = null; 
  TextView textView = null; 
  ServerSocket server = null; 
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    this.mContext = this; 
    this.textView = (TextView) this.findViewById(R.id.textView1); 
    try { 
      server = new ServerSocket(PHONE_PORT); 
    } catch (IOException e) { 
      e.printStackTrace(); 
      return; 
    } 
    new RepackTestTask().execute(); 
  } 
  private class RepackTestTask extends AsyncTask { 
    @Override 
    protected Object doInBackground(Object... params) { 
      Socket client = null; 
      // initialize server socket 
      while (true) { 
        try { 
          // attempt to accept a connection 
          client = server.accept(); 
          Log.d(TAG, "Get a connection from " 
              + client.getRemoteSocketAddress().toString()); 
          ObjectInputStream ois = new ObjectInputStream( 
              client.getInputStream()); 
          String somewords = (String) ois.readObject(); 
          Log.d(TAG, "Get some words" + somewords); 
          this.publishProgress(somewords); 
          client.close(); 
        } catch (IOException e) { 
          Log.e(TAG, "" + e); 
        } catch (ClassNotFoundException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
        } 
      } 
    } 
    @Override 
    protected void onProgressUpdate(Object... values) { 
      super.onProgressUpdate(values); 
      Toast.makeText(mContext, values[0].toString(), Toast.LENGTH_LONG) 
          .show(); 
    } 
  } 
} 

關于adb forword通信怎么在android移動端與PC端中使用就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

当涂县| 日土县| 甘孜县| 五台县| 蓝山县| 上蔡县| 获嘉县| 新乡市| 宁乡县| 宜宾市| 巴楚县| 泗阳县| 谷城县| 岳阳市| 本溪市| 长宁区| 额尔古纳市| 曲靖市| 上饶县| 泗水县| 莲花县| 静海县| 崇阳县| 丽水市| 汾西县| 北京市| 繁峙县| 广饶县| 张家口市| 永仁县| 牙克石市| 维西| 呼玛县| 邵东县| 汝州市| 宁城县| 红桥区| 河西区| 利川市| 三门峡市| 石门县|