




已阅读5页,还剩3页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Android开发_socket编程_wifi连接电脑实现PPT控制器代码还是很简单的,这里不多说了,强调一下的是,client端除了一个activity的类外,还有上面的Choices类!与服务器端的类型一模一样!同时,别忘记了需要在android manifest.XML文件中添加 1 2 用户权限!别忘记添加。当然,代码还有很多需要改进的地方,比如要解决按下可能延迟PPT没有 反应,但是又不知道是否真的按下等问题,我们可以在手机端的按钮上加上一个震动的效果,这样我们就能准确的知道我们是否按下手机上的按键。这个应该不难 吧!不过本篇文章主要还是简单介绍android socket编程与PC的连接。 标签: 代码片段(2)图片 untitled.png代码 Java代码001package nate.PPT.control;002import java.awt.AWTException;003import java.awt.Robot;004import java.awt.event.KeyEvent;005import java.io.IOException;006import java.io.ObjectInputStream;007import java.io.ObjectOutputStream;008import .ServerSocket;009import .Socket;010011012013public class PPTServer 014private final static int RIGHT = 1;015private final static int LEFT = 2;016private final static int SHIFTF5 = 0;017private final static int ESC = 3;018019private static int key;020/注意这里用的输入输出流的对象021private static ObjectInputStream fromClient;022private static ObjectOutputStream fromServer;023024public static void main(String args) throws IOException,025ClassNotFoundException, AWTException, InterruptedException026ServerSocket sSocket = new ServerSocket(2011);027System.out.println(waiting a connection from the client);028Robot robot = new Robot(); 029Socket sock = sSocket.accept();030System.out.println(recv a connection);031fromClient = new ObjectInputStream(sock.getInputStream();032fromServer = new ObjectOutputStream(sock.getOutputStream();033do034Choices choice = (Choices)fromClient.readObject();035System.out.println(the flag is + choice.getKey();036key = choice.getKey();037switch(key)038039case SHIFTF5:040robot.keyPress(KeyEvent.VK_SHIFT);041Thread.sleep(20);042robot.keyPress(KeyEvent.VK_F5);043Thread.sleep(10);044robot.keyRelease(KeyEvent.VK_F5);045robot.keyRelease(KeyEvent.VK_SHIFT);046Thread.sleep(10);047break;048049case LEFT:050robot.keyPress(KeyEvent.VK_LEFT);051Thread.sleep(10);052robot.keyRelease(KeyEvent.VK_LEFT);053Thread.sleep(10);054break;055056case RIGHT:057robot.keyPress(KeyEvent.VK_RIGHT);058Thread.sleep(10);059robot.keyRelease(KeyEvent.VK_RIGHT);060Thread.sleep(10);061break;062063case ESC:064robot.keyPress(KeyEvent.VK_ESCAPE);065Thread.sleep(10);066robot.keyPress(KeyEvent.VK_ESCAPE);067Thread.sleep(10);068break;069070default:071break;072073while(key != -1);074System.out.println(exit the app);075fromClient.close();076fromServer.close();077sock.close();078sSocket.close();079080081注 意一下所用的输入输出流对象,关于这个java中已经很清楚了,就不多说。同时,本例中使用java中的Robot来模拟按键,即PPT中的快捷 键从而实现控制PPT的目的。当然,大家都知道,使用ObjectInputStream、ObjectOutputStream传输对象首先还需下面的 条件。即传送的对象所属的类,该类必须实现Serializable接口!同时注意在android手机客户端,我们需要同样拥有这样一个类型!将此 类copy过去即可,这些都是java中的知识。082package nate.PPT.control;083084import java.io.Serializable;085086public class Choices implements Serializable087088private int key;089090public Choices(int key) 091super();092this.key = key;093094095public int getKey() 096return key;097098099public void setKey(int key) 100this.key = key;101102103104105上面类包含了传输的信息数据内容。106来看看client端的代码,部署在android手机端:107package nate.PPT.control;108109import java.io.IOException;110import java.io.ObjectInputStream;111import java.io.ObjectOutputStream;112import .InetAddress;113import .Socket;114import .UnknownHostException;115import android.app.Activity;116import android.app.AlertDialog;117import android.content.DialogInterface;118import android.content.Intent;119import android.os.Bundle;120import android.view.KeyEvent;121import android.view.View;122import android.widget.Button;123124public class PPTClient extends Activity 125private Button start;126private Button escape;127private Button forward;128private Button back;129private Socket sock;130private ObjectOutputStream fromClient;131private ObjectInputStream fromServer;132private final static int RIGHT = 1;133private final static int LEFT = 2;134private final static int SHIFTF5 = 0;135private final static int ESC = 3;136137/* Called when the activity is first created. */138Override139public void onCreate(Bundle savedInstanceState) 140super.onCreate(savedInstanceState);141setContentView(R.layout.main);142143try 144/sock = new Socket(InetAddress.getByName(99),2011);145sock = new Socket(InetAddress.getByName(65),2011);146fromClient = new ObjectOutputStream(sock.getOutputStream();147fromServer = new ObjectInputStream(sock.getInputStream();148149 catch (UnknownHostException e1) 150e1.printStackTrace();151 catch (IOException e1) 152e1.printStackTrace();153154155start = (Button)this.findViewById(R.id.start);156escape = (Button)this.findViewById(R.id.escape);157forward = (Button)this.findViewById(R.id.froward);158back = (Button)this.findViewById(R.id.back);159160start.setOnClickListener(new Button.OnClickListener()161162Override163public void onClick(View v) 164Choices choice = new Choices(SHIFTF5);165try 166fromClient.writeObject(choice);167System.out.println(send the start shift + f5);168 catch (IOException e) 169e.printStackTrace();170171172173);174175escape.setOnClickListener(new Button.OnClickListener()176177Override178public void onClick(View arg0) 179Choices choice = new Choices(ESC);180try 181fromClient.writeObject(choice);182System.out.println(send the escape);183 catch (IOException e) 184e.printStackTrace();185186187);188forward.setOnClickListener(new Button.OnClickListener()189190Override191public void onClick(View v) 192Choices choice = new Choices(RIGHT);193try 194fromClient.writeObject(choice);195System.out.println(send the right (the next);196 catch (IOException e) 197e.printStackTrace();198199200201202);203back.setOnClickListener(new Button.OnClickListener()204205Override206public void onClick(View v) 207Choices choice = new Choices(LEFT);208try 209fromClient.writeObject(choice);210System.out.println(send the left (the last);211 catch (IOException e) 212e.printStackTrace();213214215);216217218/*219* 监听BACK键220* param keyCode221* param event222* return223*/224public boolean onKeyDown(int keyCode, KeyEvent event) 225 226if ( event.getKeyCode() = KeyEvent.KEYCODE_BACK)227AlertDialog.Builder builder = new AlertDialog.Builder(this);228builder.setTitle(ex
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 生活补助合同协议
- 猪苗包销合同协议
- 销售粉煤合同协议
- 影视顾问合同协议
- 微信分销合同协议
- 指标买卖合同协议
- 蚊帐买卖合同协议
- 设备进货合同协议
- 药房承包合同协议
- 物业工厂合同协议
- 药事管理法律法规相关知识培训
- 地毯织造技艺(北京宫毯织造技艺)
- 第4章-选区激光熔化工艺及材料课件
- GB/T 3785.1-2023电声学声级计第1部分:规范
- 2023届高考写作指导:“寻找温暖”与“成为灯火”课件
- 2022年上海市工业技术学校招聘考试真题
- 长期护理保险技能比赛理论试题库300题(含各题型)
- 二重积分的概念与性质演示文稿
- 医院双重预防机制建设工作完成情况
- 大学生劳动教育通论知到章节答案智慧树2023年大连海洋大学
- 2003高教社杯全国大学生数学建模竞赛B题竞赛参考答案
评论
0/150
提交评论