- 6
- 0
- 约3.75万字
- 约 38页
- 2021-08-21 发布于江苏
- 举报
PC客户端与Android服务端的Socket同步通信(USB)收藏需求:
.一个androi端d
的servic后e
台运行的程序,作为socket的服务器端;用于接收Pcclient
端发来的命令,来处理数据后,把结果发给PCclient
PC端程序,作为socket的客户端,用于给android手机端发操作命令难点分析:
1.手机一定要有adb模式,即插上USB线时马上提示的对话框选adb。好多对手机的操作都可以用adb直接作。
不过,我发现LGGW880就没有,要去下载个2.androi默d认手机端的IP为“”
.要想联通PC与android手机的sokce,t一定要用adbforward来作下端口转发才能连上socket.
viewplaincopytoclipboardprint?Runtime.getRuntime().exec(adbforwardtcp:12580tcp:10086);
Thread.sleep(3000);Runtime.getRuntime().exec(adbforwardtcp:12580tcp:10086);
Thread.sleep(3000);
androi端d的servic程e序Instal到l手机上容易,但是还要有方法来从PC的client
端来启动手机上的service,这个办法可以通过PC端adb命令来发一个Broastcas,t手机
端再写个接收BroastcastRecei来ve接收这个Broastcas,t在这个BroastcastRecei来ve启动service
pc端命令:
viewplaincopytoclipboardprint?
Runtime.getRuntime().exec(
adbshellambroadcast-aNotifyServiceStart);Runtime.getRuntime().exec(
adbshellambroadcast-aNotifyServiceStart);android端的代码:ServiceBroadcastReceiver.javaviewplaincopytoclipboardprint?
packagecom.otheri.service;
importandroid.content.BroadcastReceiver;importandroid.content.Context;
importandroid.content.Intent;importandroid.util.Log;
publicclassServiceBroadcastReceiverextendsBroadcastReceiver{privatestaticStringSTART_ACTION=NotifyServiceStart;privatestaticStringSTOP_ACTION=NotifyServiceStop;
@Override
publicvoidonReceive(Contextcontext,Intentintent){Log.d(androidService.,TATGhread.currentThread().getName()+
+ServiceBroadcastReceiveronReceive);
Stringaction=intent.getAction();
if(START_ACTION.equalsIgnoreCase(action)){context.startService(newIntent(context,androidService.class));
Log.d(androidService.,TATGhread.currentThread().getName()+
+ServiceBroadcastReceiveronReceivestartend);
}elseif(STOP_ACTION.equalsIgnoreCase(action)){context.stopService(newIntent(context,androidService.class));Log.d(androidService.,TATGhread.currentThread().getName()+
+ServiceBroadcastReceiveronReceivestopend);
}
}
}
packagecom.otheri.service;
importandroid.content.BroadcastReceiver;importandroid.content.Context;
importandroid.content.Intent;importandroid.util.Log
原创力文档

文档评论(0)