欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

android手机端与PC端使用adb forword通信

时间:2021-01-01 15:18:01|栏目:Android代码|点击:

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(); 
    } 
  } 
} 

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:Phonegap使用拍照功能时的内存问题

栏    目:Android代码

下一篇:Android笔记之:App应用之发布各广告平台版本的详解

本文标题:android手机端与PC端使用adb forword通信

本文地址:http://www.codeinn.net/misctech/39278.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有