把socket放在service里面,只能给服务器发送一次数据

lzdzxd 2016-09-14 08:13:46
想把socket通信放在service里面,服务器端一直在监听,但是服务器端只能接受到第一次客户端发送的数据。
service代码:
package com.example.test;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class SocketService extends Service {

//Socket通信
private final String HOST = "192.168.0.195";
private static final String TAG = "SocketService";
private final int PORT = 30002;
public static Socket socket; //这里定义了一个静态socket 供其他activity使用

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onCreate() {
super.onCreate();
Log.v(TAG, "onCreate");

}

@Override
public void onDestroy() {
super.onDestroy();
Log.v(TAG, "onDestroy");
}

@Override
public void onStart(Intent intent, int startId) {
Log.v(TAG, "onStart");
new Thread() {
@Override
public void run() {
// if (socket.isConnected() == true)
initSocket();
}
}.start();
}

private void initSocket() {
try {
Log.d("Thread", "connect the server by the socket in service");
socket = new Socket(HOST, PORT);
}
catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
client代码:
package com.example.test;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends Activity {

private char msg = '3';
private boolean flag = true;
private PrintWriter finout = null;

class Mythread extends Thread{

@Override
public void run() {
int cnt = 10;
System.out.println("Out of the thread");
while (cnt > 0){
try {
//直接调用Service中的socket
// System.out.println("In the thread");
finout = new PrintWriter(new BufferedWriter(new OutputStreamWriter(SocketService.socket.getOutputStream())), true);

} catch (Exception e){
e.printStackTrace();
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.record_voice);
//开启socket service服务
Intent i = new Intent(MainActivity.this, SocketService.class);
i.putExtra("SocketService", "SurvingwithAndroid");
MainActivity.this.startService(i);
final Mythread thread1 = new Mythread();
thread1.start();

View record_voice_begin_control = (ImageButton)findViewById(R.id.record_voice_begin);
Button record_voice_fanhui_control = (Button)findViewById(R.id.record_voice_fanhui);

//开始录音
record_voice_begin_control.setOnTouchListener(new View.OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
System.out.println("key_down");
msg ='2';
if (SocketService.socket.isConnected()){
System.out.println("in the first first if");
if (!SocketService.socket.isOutputShutdown()){
System.out.println("in the first second if");
finout.println(msg);
}
}
break;
case MotionEvent.ACTION_UP:
System.out.println("key_up");
msg = '3';
if (SocketService.socket.isConnected()){
System.out.println("in the second first if");
if (!SocketService.socket.isOutputShutdown()){
System.out.println("in the second second if");
finout.println(msg);
}
}
break;
}
return true;
}
});

//结束应用
record_voice_fanhui_control.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
Toast tst = Toast.makeText(MainActivity.this, "App终止", Toast.LENGTH_SHORT);
tst.show();
System.out.println("App终止");
//返回到功能菜单界面
System.out.println("interrupt_record_voice");
// MainActivity.this.stopService(i);
Intent intent = new Intent(MainActivity.this, SocketService.class);
stopService(intent);
thread1.interrupt();
MainActivity.this.finish();

}
});
}
}
求高手帮忙看看,谢谢
...全文
557 4 打赏 收藏 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
狙击超超 2018-06-21
  • 打赏
  • 举报
回复
我和你一样,只发送了一次心跳,就执行onClose()方法了
lzdzxd 2016-09-15
  • 打赏
  • 举报
回复
非常感谢两位
Chary Liu 2016-09-15
  • 打赏
  • 举报
回复
线程没有循环,当然只接收了一次就结束了
网络咖啡 2016-09-15
  • 打赏
  • 举报
回复
服务里面的线程run方法里面加个循环,不然run方法立刻执行结束,就退出线程了

80,490

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧