I am creating one client server application in which android application works as server. client code is written in java. what i want to do is listen for connection continuously in android application's background while user can continue doing other things in application.
part of java client code is
Socket socket1 = null;
DataOutputStream dataOutputStream = null;
DataInputStream dataInputStream = null;
socket1 = new Socket(GlobalState.waiterip, 8888);
dataOutputStream = new DataOutputStream(socket1.getOutputStream());
dataInputStream = new DataInputStream(socket1.getInputStream());
dataOutputStream.writeUTF("1");
android activity code is
Button tabstatus,menu,logout,inbox;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.waiterhome);
tabstatus=(Button)findViewById(R.id.tabstatus);
menu=(Button)findViewById(R.id.menu);
logout=(Button)findViewById(R.id.logout);
inbox=(Button)findViewById(R.id.inbox);
Intent myIntent = new Intent(getApplicationContext(),WaiterService.class);
startService(myIntent);
tabstatus.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//some code }
});
menu.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//some code
}
});
logout.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//some code
});
inbox.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//some code
}
});
}
}
code for android service that i m starting i.e. WaiterService is
public class WaiterService extends Service
{
public IBinder onBind(Intent intent)
{
return null;
}
public void onCreate()
{
ServerSocket serverSocket = null;
Socket socket = null;
DataInputStream dataInputStream = null;
DataOutputStream dataOutputStream = null;
try
{
serverSocket = new ServerSocket(8888);
while(true)
{
socket = serverSocket.accept();
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream = new DataOutputStream(socket.getOutputStream());
int op=Integer.parseInt(dataInputStream.readUTF());
switch(op)
{
case 1://some code
break;
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
I have also included description of service in manifest.xml file
when I run this code,I get errors in logcat as follow
04-07 12:23:53.393: W/ActivityManager(51): Launch timeout has expired, giving up wake lock!
04-07 12:23:53.399: W/ActivityManager(51): Activity idle timeout for HistoryRecord{44e00278 aac.imenu.MyDroidClient/.WaiterActivity}
04-07 12:24:03.510: W/ActivityManager(51): Timeout executing service: ServiceRecord{44dde9b0 aac.imenu.MyDroidClient/.WaiterService}
and emulator screen turns black and no gui for activity is shown.also socket communication does not take place.
I don't know where exactly the problem is, Pls guide me how to solve this problem. or is there any other method for continuous socket listening in background.