(售后服务)PC客户端与A服务端的S同步通信

上传人:管****问 文档编号:119812600 上传时间:2020-01-26 格式:DOC 页数:38 大小:185.54KB
返回 下载 相关 举报
(售后服务)PC客户端与A服务端的S同步通信_第1页
第1页 / 共38页
(售后服务)PC客户端与A服务端的S同步通信_第2页
第2页 / 共38页
(售后服务)PC客户端与A服务端的S同步通信_第3页
第3页 / 共38页
(售后服务)PC客户端与A服务端的S同步通信_第4页
第4页 / 共38页
(售后服务)PC客户端与A服务端的S同步通信_第5页
第5页 / 共38页
点击查看更多>>
资源描述

《(售后服务)PC客户端与A服务端的S同步通信》由会员分享,可在线阅读,更多相关《(售后服务)PC客户端与A服务端的S同步通信(38页珍藏版)》请在金锄头文库上搜索。

1、 PC客户端与Android服务端的Socket同步通信(USB) 收藏 需求: 1.一个android端的service后台运行的程序,作为socket的服务器端;用于接收Pc client端发来的命令,来处理数据后,把结果发给PC client 2.PC端程序,作为socket的客户端,用于给android手机端发操作命令难点分析: 1.手机一定要有adb模式,即插上USB线时马上提示的对话框选adb。好多对手机的操作都可以用adb直接作。 不过,我发现LG GW880就没有,要去下载个 2.android默认手机端的IP为“127.0.0.1” 3.要想联通PC与android手机的so

2、kcet,一定要用adb forward 来作下端口转发才能连上socket.view plaincopy to clipboardprint?Runtime.getRuntime().exec(adb forward tcp:12580 tcp:10086); Thread.sleep(3000); Runtime.getRuntime().exec(adb forward tcp:12580 tcp:10086);Thread.sleep(3000); 4.android端的service程序Install到手机上容易,但是还要有方法来从PC的client端来启动手机上的service ,

3、这个办法可以通过PC端adb命令来发一个Broastcast ,手机端再写个接收BroastcastReceive来接收这个Broastcast,在这个BroastcastReceive来启动service pc端命令: view plaincopy to clipboardprint?Runtime.getRuntime().exec( adb shell am broadcast -a NotifyServiceStart); Runtime.getRuntime().exec(adb shell am broadcast -a NotifyServiceStart); android端的

4、代码:ServiceBroadcastReceiver.java view plaincopy to clipboardprint?package com.otheri.service; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; public class ServiceBroadcastReceiver extends BroadcastReceiver private stat

5、ic String START_ACTION = NotifyServiceStart; private static String STOP_ACTION = NotifyServiceStop; Override public void onReceive(Context context, Intent intent) Log.d(androidService.TAG, Thread.currentThread().getName() + - + ServiceBroadcastReceiver onReceive); String action = intent.getAction();

6、 if (START_ACTION.equalsIgnoreCase(action) context.startService(new Intent(context, androidService.class); Log.d(androidService.TAG, Thread.currentThread().getName() + - + ServiceBroadcastReceiver onReceive start end); else if (STOP_ACTION.equalsIgnoreCase(action) context.stopService(new Intent(cont

7、ext, androidService.class); Log.d(androidService.TAG, Thread.currentThread().getName() + - + ServiceBroadcastReceiver onReceive stop end); package com.otheri.service;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.util.Log;public c

8、lass ServiceBroadcastReceiver extends BroadcastReceiver private static String START_ACTION = NotifyServiceStart;private static String STOP_ACTION = NotifyServiceStop;Overridepublic void onReceive(Context context, Intent intent) Log.d(androidService.TAG, Thread.currentThread().getName() + -+ ServiceB

9、roadcastReceiver onReceive);String action = intent.getAction();if (START_ACTION.equalsIgnoreCase(action) context.startService(new Intent(context, androidService.class);Log.d(androidService.TAG, Thread.currentThread().getName() + -+ ServiceBroadcastReceiver onReceive start end); else if (STOP_ACTION.

10、equalsIgnoreCase(action) context.stopService(new Intent(context, androidService.class);Log.d(androidService.TAG, Thread.currentThread().getName() + -+ ServiceBroadcastReceiver onReceive stop end); 5.由于是USB连接,所以socket就可以设计为一但连接就一直联通,即在new socket和开完out,in流后,就用个while(true)来循环PC端和android端的读和写 android的代码

11、:view plaincopy to clipboardprint?public void run() Log.d(androidService.TAG, Thread.currentThread().getName() + - + a client has connected to server!); BufferedOutputStream out; BufferedInputStream in; try /* PC端发来的数据msg */ String currCMD = ; out = new BufferedOutputStream(client.getOutputStream();

12、 in = new BufferedInputStream(client.getInputStream(); / testSocket();/ 测试socket方法 androidService.ioThreadFlag = true; while (androidService.ioThreadFlag) try if (!client.isConnected() break; /* 接收PC发来的数据 */ Log.v(androidService.TAG, Thread.currentThread().getName() + - + will read.); /* 读操作命令 */ currCMD = readCMDFromSocket(in); Log.v(androidService.TAG, Thread.currentThread().getName() + - + *currCMD = + currCMD); /* 根据命令分别处理数据 */ if (currCMD.equals(1) out

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 商业/管理/HR > 经营企划

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号