我创建了一个服务,该服务又创建了一个套接字,但是一旦尝试读取从服务器发送的数据,它就会挂起,另一方面,如果我不使用服务而是使用普通活动,则相同的代码会运行?
import android.app.*;
import android.content.*;
import android.os.*;
import android.util.*;
import java.net.*;
import java.io.*;
import android.widget.*;
public class BackgroundService extends Service
{
private Socket socket=null;
private InputStreamReader in=null;
private String ip;
private String tag="BackgroundService";
public IBinder onBind(Intent p1)
{
// TODO: Implement this method
return null;
}
public void onCreate(){
super.onCreate();
}
public void onStart(Intent i,int id){
super.onStart(i,id);
ip=i.getStringExtra("Ip");
Log.v(tag,"Ip "+i.getStringExtra("Ip"));
try
{
socket = new Socket(ip.trim(), 8888);
new Runnable() {
public void run()
{
// Hangs in this block.....
try
{
in = new InputStreamReader (socket.getInputStream());
BufferedReader bf=new BufferedReader (in);
// Never Reaches this messge
Log.v(tag,"mess:"+bf.readLine() );
}
catch (IOException e)
{
e.printStackTrace();
}
}
}.run();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}