欢迎来到天天文库
浏览记录
ID:38341107
大小:202.63 KB
页数:37页
时间:2019-06-10
《PC客户端与Android服务端的Socket同步通信(2)》由会员上传分享,免费在线阅读,更多相关内容在学术论文-天天文库。
1、PC客户端与Android服务端的Socket同步通信(USB)1.一个android端的service后台运行的程序,作为socket的服务器端;用于接收Pcclient端发来的命令,来处理数据后,把结果发给PCclient2.PC端程序,作为socket的客户端,用于给android手机端发操作命令难点分析:1.手机一定要有adb模式,即插上USB线时马上提示的对话框选adb。好多对手机的操作都可以用adb直接作。不过,我发现LGGW880就没有,要去下载个2.android默认手机端的IP为“127.
2、0.0.1”3.要想联通PC与android手机的sokcet,一定要用adbforward来作下端口转发才能连上socket.viewplaincopytoclipboardprint?Runtime.getRuntime().exec("adbforwardtcp:12580tcp:10086");Thread.sleep(3000);Runtime.getRuntime().exec("adbforwardtcp:12580tcp:10086");Thread.sleep(3000);4.androi
3、d端的service程序Install到手机上容易,但是还要有方法来从PC的client端来启动手机上的service,这个办法可以通过PC端adb命令来发一个Broastcast,手机端再写个接收BroastcastReceive来接收这个Broastcast,在这个BroastcastReceive来启动servicepc端命令:viewplaincopytoclipboardprint?Runtime.getRuntime().exec("adbshellambroadcast-aNotifyServ
4、iceStart");Runtime.getRuntime().exec("adbshellambroadcast-aNotifyServiceStart");android端的代码:ServiceBroadcastReceiver.javaviewplaincopytoclipboardprint?packagecom.otheri.service;importandroid.content.BroadcastReceiver;importandroid.content.Context;importand
5、roid.content.Intent;importandroid.util.Log;publicclassServiceBroadcastReceiverextendsBroadcastReceiver{privatestaticStringSTART_ACTION="NotifyServiceStart";privatestaticStringSTOP_ACTION="NotifyServiceStop";@OverridepublicvoidonReceive(Contextcontext,Inten
6、tintent){Log.d(androidService.TAG,Thread.currentThread().getName()+"---->"+"ServiceBroadcastReceiveronReceive");Stringaction=intent.getAction();if(START_ACTION.equalsIgnoreCase(action)){context.startService(newIntent(context,androidService.class));Log.d(an
7、droidService.TAG,Thread.currentThread().getName()+"---->"+"ServiceBroadcastReceiveronReceivestartend");}elseif(STOP_ACTION.equalsIgnoreCase(action)){context.stopService(newIntent(context,androidService.class));Log.d(androidService.TAG,Thread.currentThread(
8、).getName()+"---->"+"ServiceBroadcastReceiveronReceivestopend");}}}packagecom.otheri.service;importandroid.content.BroadcastReceiver;importandroid.content.Context;importandroid.content.Intent;importandroid.ut
此文档下载收益归作者所有