How to add a listener to a running service on Android
<p>I added a listener to the service called "BindService".<br>
And then I killed the Activity which have added the listener to the "BindService".<br>
"BindService" is still running. I can see it on DDMS of eclipse.<br>
Now I started the Activity again and I want to add a listener to the "BindService" again.<br>
How could I?<br><br>
"BindService" is just a service which is counting number.<br><br>
With the following code, after I start the Activity again and tap the button "button_sendwords", it starts to count number from 0 again. That is not my purpose.<br></p>

<pre><code>public class MainActivity extends Activity {

private ICallbackService service;
private static final int CALLBACK_MESSAGE = 1;

private Handler handler = new Handler(){
    @Override
    public void dispatchMessage(Message msg){
        if(msg.what == CALLBACK_MESSAGE){
            TextView resultlbl = (TextView)findViewById(R.id.label_result);
            resultlbl.setText((String)msg.obj);
        }else{
            super.dispatchMessage(msg);
        }
    }
};

private ICallbackListener listener = new ICallbackListener.Stub() {
    @Override
    public void receiveMessage(String message) throws RemoteException {
        handler.sendMessage(handler.obtainMessage(CALLBACK_MESSAGE, message));
    }
};

private ServiceConnection conn = new ServiceConnection(){

    @Override
    public void onServiceConnected(ComponentName name, IBinder binder) {
        service = ICallbackService.Stub.asInterface(binder);
        try{
            service.addListener(listener);
        }catch(RemoteException e){
        }
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
    }

};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button btnSendWords = (Button)findViewById(R.id.button_sendwords);

    btnSendWords.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, BindService.class);
            bindService(intent, conn, BIND_AUTO_CREATE);
        }
    });
}    
</code></pre>

<p>}</p>
