public void onServiceConnected(ComponentName name, IBinder service) {
Log.i( "Service connection established","");
// that's how we get the client side of the IPC connection
api = com.oreilly.android.otweet.TweetCollectorApi.Stub.asInterface(service);
try {
api.addListener(collectorListener);
/* Intent i = new Intent(StatusListActivity.this, StatusListActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);*/
} catch (RemoteException e) {
Log.e("", "Failed to add listener"开发者_如何转开发, e);
}
To stop service when application stops we need to call unbindService()
in the onDestroy()
.
Edit:
In your link there are code for this
@Override
protected void onDestroy() {
super.onDestroy();
try {
api.removeListener(collectorListener);
unbindService(serviceConnection);
} catch (Throwable t) {
// catch any issues, typical for destroy routines
// even if we failed to destroy something, we need to continue destroying
Log.w(TAG, "Failed to unbind from the service", t);
}
Log.i(TAG, "Activity destroyed");
}
精彩评论