android手机端与PC端使用adb forword通信
程序员文章站
2022-06-20 13:11:13
pc端与android手机端使用adb forword通信
服务器端代码如下:
import java.io.ioexception;
import jav...
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(); } } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
下一篇: 孙尚香与刘备的悲剧婚姻是谁导致的?