I am starting two asynchronous task in two diffrent services first one works correctly and opens a socket but second one is not starting here is the code my application is based on client socket programing in wifip2p.
public class RecieveAudioService extends Service {
String tag = "Recieve Audio Service";
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getApplicationContext(), "Recieve audio :requesting to client", Toast.LENGTH_LONG).show();
String[] param = {"h", "j", "k"};
new request().execute();
Log.v("Recieve Audio", "Inside Recieve audio service");
return super.onStartCommand(intent, flags, startId);
}
//===================== This class is sending request to server for connection when invocked
public class request extends AsyncTask<String, String, String> {
protected String doInBackground(String... arg0) {
Log.v("second asyn", "this is second asynchronous task");
try {
Toast.makeText(getApplicationContext(), "Request to connect sent to server",
Toast.LENGTH_LONG).show();
String toConnectDeviceIP = "192.168.49.1";
Integer toConnectDevicePort = 8988;
Socket connect = new Socket();
connect.bind(null);
connect.connect((new InetSocketAddress(toConnectDeviceIP, toConnectDevicePort)),
5000);
Log.v(tag, "sent the connection request to clint");
connect.close();
} catch (Exception e) {
// TODO Auto-generated catch block
Log.v(tag, "" + e.toString());
Toast.makeText(getApplicationContext(), "i found exception in connection"
+e.toString(), Toast.LENGTH_LONG).show();
}
return "success";
}
}
//====================================================================
}
It seems that you launch your app on Android 4.x/3.x. Before Android 3.0 AsyncTasks
ran concurrently but later versions(>3.x) of system executes tasks one after another. So you have two choices:
a) Use Threads
instead of AsyncTasks
b) Use executeOnExecutor
of AsyncTask
to execute tasks concurrently:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
task.execute();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.