Based on the assumption in the previous question that I asked here, I am trying to push the socket for the application that I wrote to the service. Yesterday I spent most of the day researching services, and actually mocked several (one remote, one local).
My question has two parts:
1) after playing with both the local service and the remote service, I'm still not sure which one is best for my situation. This is largely due to the fact that I guess that I still do not quite understand what advantages will be fulfilled in another "process". I am creating a new thread for connecting sockets, regardless of the fact that I will not have conflicts with the user interface. So what does the installation do in another process? Can I potentially see better performance this way? My limited understanding is that by placing it in another process, the service will work no matter what work I do in my application. I have several different actions, but only one of them requires a socket connection, which I will rebuild every time,when activity opens. So will the local service become for me?
2) I will have my listener socket (DataInputStream (). ReadLine () inside the while loop) inside my service for any new data that is being transferred from the server. After yesterday’s game, I couldn’t understand how to transfer the data that it reads to the actual “client” (either the connected client by the remote service or the local client itself) in “real time”.
I would really appreciate some suggestions for part 1 and some help with part 2 (code examples? :))
TIA
Edit: added my service code - with local service
Class of service:
public class SocketService extends Service {
Socket s;
PrintStream os;
@Override
public IBinder onBind(Intent arg0) {
return myBinder;
}
private final IBinder myBinder = new LocalBinder();
public class LocalBinder extends Binder {
public SocketService getService() {
return SocketService.this;
}
}
@Override
public void onCreate() {
super.onCreate();
s = new Socket();
}
public void IsBoundable(){
Toast.makeText(this,"I bind like butter", Toast.LENGTH_LONG).show();
}
public void onStart(Intent intent, int startId){
super.onStart(intent, startId);
Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();
Runnable connect = new connectSocket();
new Thread(connect).start();
}
class connectSocket implements Runnable {
@Override
public void run() {
SocketAddress socketAddress = new InetSocketAddress("192.168.1.104", 4505);
try {
s.connect(socketAddress);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
try {
s.close();
} catch (IOException e) {
e.printStackTrace();
}
s = null;
}
}
Actions calling the service:
public class SocketServiceController extends Activity {
private SocketService mBoundService;
private Boolean mIsBound;
public SocketServiceController ssc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ssc = this;
setContentView(R.layout.main);
Button start = (Button)findViewById(R.id.serviceButton);
Button stop = (Button)findViewById(R.id.cancelButton);
start.setOnClickListener(startListener);
stop.setOnClickListener(stopListener);
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
mBoundService = ((SocketService.LocalBinder)service).getService();
}
public void onServiceDisconnected(ComponentName className) {
mBoundService = null;
}
};
private void doBindService() {
bindService(new Intent(SocketServiceController.this, SocketService.class), mConnection, Context.BIND_AUTO_CREATE);
mIsBound = true;
mBoundService.IsBoundable();
}
private void doUnbindService() {
if (mIsBound) {
unbindService(mConnection);
mIsBound = false;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
doUnbindService();
}
private OnClickListener startListener = new OnClickListener() {
public void onClick(View v){
startService(new Intent(SocketServiceController.this,SocketService.class));
doBindService();
}
};
private OnClickListener stopListener = new OnClickListener() {
public void onClick(View v){
stopService(new Intent(SocketServiceController.this,SocketService.class));
}
};
}