文档介绍:PC 客户端与 Android 服务端的 Socket 同步通信(USB)
android 端的 service 后台运行的程序,作为 socket 的服务器端;用于接收 Pc client
端发来的命令,来处理数据后,把结果发给 PC client
端程序,作为 socket 的客户端,用于给 android 手机端发操作命令
难点分析:
adb 模式,即插上 USB 线时马上提示的对话框选 adb。好多对手机的
操作都可以用 adb 直接作。
不过,我发现 LG GW880 就没有,要去下载个
默认手机端的 IP 为“”
PC 与 android 手机的 sokcet,一定要用 adb forward 来作下端口转发才能连
上 socket.
view plaincopy to clipboardprint?
().exec("adb forward tcp:12580 tcp:10086");
(3000);
().exec("adb forward tcp:12580 tcp:10086");
(3000);
端的 service 程序 Install 到手机上容易,但是还要有方法来从 PC 的 client
端来启动手机上的 service ,这个办法可以通过 PC 端 adb 命令来发一个 Broastcast ,手机
端再写个接收 BroastcastReceive 来接收这个 Broastcast,在这个 BroastcastReceive 来启动
service
pc 端命令:
view plaincopy to clipboardprint?
().exec(
"adb shell am broadcast -a NotifyServiceStart");
().exec(
"adb shell am broadcast -a NotifyServiceStart");
android 端的代码:
view plaincopy to clipboardprint?
.;
import ;
import ;
import ;
import ;
public class ServiceBroadcastReceiver extends BroadcastReceiver {
private static String START_ACTION = "NotifyServiceStart";
private static String STOP_ACTION = "NotifyServiceStop";
***@Override
public void onReceive(Context context, Intent intent) {
(, ().getName() + "---->"
+ "ServiceBroadcastReceiver onReceive");
String action = ();
if ((action)) {
(new Intent(context, ));
(, ().getName() + "---->"
+ "ServiceBroadcastReceiver onReceive start end");
} else if ((action)) {