I have a server running on an android device as a Service. I want the user to enter the clients number and then the service starts. Is there a better way to take care of this instead of passing data from an activity to a Service?
Furthermore, when i use the above approach it seems that the client hangs and forces the application to destroy. Here is the code:
String a = clients.getText().toString();
Bundle bundle = new Bundle();
bundle.putCharSequence("NumberOfClients", a);
Intent intent = new Intent(Manage.this, Server.class);
intent.putExtras(bundle);
Log.d("hi", a);
startService(intent);
And here is the Server:
@Override
public void onCreate()
{
Thread server = new Thread(new ServerThread());
server.start();
}
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Bundle bundle = new Bundle();
bundle = intent.getExtras();
numberofclients = (String) bundle.getCharSequence("NumberOfClients");
int a = Integer.parseInt(numberofclients);
return a;
}
When I hit the button to connect to the server the client hangs. Why is this happening?
onStartCommand is supposed to return a predefined value on how to treat the service.
It looks like you’d want a START_NOT_STICKY
So in your return statement in onStartCommand, that’s what you’d be returning. i.e.
If you want to save your integer a for use somewhere else in the service, create a global variable and set it equal to a.
Refer to Android Docs on Service
http://developer.android.com/reference/android/app/Service.html#START_CONTINUATION_MASK