下面手机端代码
package com.example.mypptcontrol;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button loginButton1;
private Button loginButton2;
private Button loginButton3;
private Button loginButton4;
private Button loginButton5;
PrintWriter pw = null;
Socket socket=null;
OutputStream os=null;
private Thread newThread;
public String ppt;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化连接按钮
loginButton1 =(Button) findViewById(R.id.button2);
loginButton2 =(Button) findViewById(R.id.button5);
loginButton3 =(Button) findViewById(R.id.button6);
loginButton4 =(Button) findViewById(R.id.button4);
loginButton5 =(Button) findViewById(R.id.button1);
EditText et = (EditText)findViewById(R.id.editText1);
//获取edittext文本内容
String value1 = et.getText().toString();
EditText et2 = (EditText)findViewById(R.id.editText2);
//监听按钮一 设置点击事件
loginButton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// 创建线程
newThread = new Thread(new Runnable() {
@Override
public void run() {
try {
//创建socket连接
Socket socket=new Socket("123.150.225.59",7777);
OutputStream os=socket.getOutputStream();
PrintWriter pw= new PrintWriter(os);
} catch (UnknownHostException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
});
//启动线程
newThread.start();
}
});
//监听按钮二 设置点击事件
loginButton2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO 自动生成的方法存根
ppt="fullscreen";
}
});
//监听按钮三 设置点击事件
loginButton3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO 自动生成的方法存根
ppt="last";
}
});
//监听按钮四 设置点击事件
loginButton4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO 自动生成的方法存根
ppt="next";
}
});
//监听按钮五 设置点击事件
loginButton5.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO 自动生成的方法存根
ppt="final";
}
});
//传送控制字符串
while(ppt!=null){
if(ppt.equals("final")){
pw.write("final");
pw.flush();
try {
pw.write("final");
pw.flush();
os.close();
socket.close();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
if(ppt.equals("next")){
pw.write("next");
pw.flush();
}
if(ppt.equals("stop")){
pw.write("stop");
pw.flush();
}
if(ppt.equals("last")){
pw.write("last");
pw.flush();
}
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
下面是服务端代码
package com.pptcontrol;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
public static void main(String[] args) {
// TODO 自动生成的方法存根
try {
//创建服务器端Socket,绑定端口,监听
System.out.println("*****服务器启动了!等待客户端连接!*****");
InetAddress addr1 = InetAddress.getLocalHost();
System.out.println("服务器名称地址:"+addr1);
ServerSocket serverScoket=new ServerSocket(7777);
Socket socket=serverScoket.accept();
System.out.println("*****成功连接*****");
//接受数据
InputStream is=socket.getInputStream();
InputStreamReader isr=new InputStreamReader(is);
BufferedReader br=new BufferedReader(isr);
String info=null;
info=br.readLine();
//判断控制字符串 模拟键盘输入
while(info!=null){
if(info.equals("fullscreen")){
robot.keyPress(KeyEvent.VK_F6);
robot.keyRelease(KeyEvent.VK_F6);
System.out.println("*****全屏执行*****");
}else if(info.equals("next")){
robot.keyPress(KeyEvent.VK_N);
robot.keyRelease(KeyEvent.VK_N);
System.out.println("*****下一页执行*****");
}else if(info.equals("last")){
robot.keyPress(KeyEvent.VK_P);
robot.keyRelease(KeyEvent.VK_P);
System.out.println("*****上一页执行*****");
}else if(info.equals("stop")){
robot.keyPress(KeyEvent.VK_ESCAPE);
robot.keyRelease(KeyEvent.VK_ESCAPE);
System.out.println("*****停止执行*****");
}else if(info.equals("final")){
socket.shutdownInput();
isr.close();
is.close();
socket.close();
serverScoket.close();
System.out.println("*****服务器已关闭*****");
}
}
}
catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
private static Robot robot = null;
public void RobotDemo()
{
try
{
robot = new Robot();
}
catch (AWTException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
现在就是连接之后 会打印出连接成功 但是安卓上 按上一页下一页之类的按钮没有反应 按结束也没反应 求高手看看