1

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.

chintansavla
  • 49
  • 3
  • 8

2 Answers2

0

A possible reason could be that your server socket is blocking the main thread. Try putting the server socket listening in seperate process.

Tech Agent
  • 657
  • 5
  • 12
  • what to u exactly mean by putting it in a separate process?? – chintansavla Apr 07 '12 at 08:20
  • The components can be run in separate process by android:process attribute. However, on second thought it seems that perhaps running it in a separate thread may serve the purpose. – Tech Agent Apr 07 '12 at 08:42
  • tried various thread combinations but still no success! also tried asynctask. please suggest me a solution. – chintansavla Apr 07 '12 at 12:39
0

I think you can ignore this error since this might not be the cause of the problem.

Check the question here

Community
  • 1
  • 1
BlackberryChennai
  • 436
  • 1
  • 5
  • 18