adb forword通信怎么在android移动端与PC端中使用

发布时间:2020-12-03 16:27:40 作者:Leah
来源:亿速云 阅读:125

这篇文章给大家介绍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端中使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. adb 连接时候不弹出授权对话框
  2. adb shell 入门

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android adb forword fo

上一篇:怎么在Android应用中实现一个全局返回功能

下一篇:pendingIntent与Intent在Android中的区别有什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》