android手機(jī)端與PC端使用adb forword通信
更新時間:2017年04月21日 10:35:38 投稿:lqh
這篇文章主要介紹了android手機(jī)端與PC端使用adb forword通信的相關(guān)資料,需要的朋友可以參考下
PC端與Android手機(jī)端使用adb forword通信
服務(wù)器端代碼如下:
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();
}
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android 判斷當(dāng)前語言環(huán)境是否是中文環(huán)境
本文主要介紹了Android 判斷當(dāng)前語言環(huán)境是否是中文環(huán)境的方法。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04
Android利用Fragment實現(xiàn)Tab選項卡效果
這篇文章主要為大家詳細(xì)介紹了Android利用Fragment實現(xiàn)Tab選項卡效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-08-08
詳解Android應(yīng)用開發(fā)--MP3音樂播放器代碼實現(xiàn)(一)
這篇文章主要介紹了詳解Android應(yīng)用開發(fā)--MP3音樂播放器代碼實現(xiàn)(一),非常具有實用價值,需要的朋友可以參考下 。2017-01-01
Android 實現(xiàn)無網(wǎng)絡(luò)傳輸文件的示例代碼
本篇文章主要介紹了Android 實現(xiàn)無網(wǎng)絡(luò)傳輸文件的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-02-02
Android+SQLite數(shù)據(jù)庫實現(xiàn)的生詞記事本功能實例
這篇文章主要介紹了Android+SQLite數(shù)據(jù)庫實現(xiàn)的生詞記事本功能,結(jié)合具體實例形式分析了Android操作SQLite數(shù)據(jù)庫實現(xiàn)生詞記錄功能的操作步驟與相關(guān)注意事項,需要的朋友可以參考下2017-09-09

