I've created a REALLY simple service and a really simple client. But, I see 
some strange behavior. After I call unbindService and the service's 
onDestroy method is called (as evidenced by a log message or toast), I can 
still query the service.
1. When the service and client are bound, I can't see the service in the 
Running Services panel
This has to be something stupid that I'm not seeing. Thanks in advance.

public class MyWeatherService extends Service {

  int count = 1;

  Messenger messengerFromClient = null;
  IncomingHandler incomingHandler = new IncomingHandler();
  Messenger messengerToClient = new Messenger(
      incomingHandler);

  class IncomingHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
      messengerFromClient = msg.replyTo;
      Bundle reply = new Bundle();

      reply.putString("weather", "It's dark at night."
          + Integer.toString(count++));
      Message replyMessage = Message.obtain();
      replyMessage.setData(reply);
      try {
        messengerFromClient.send(replyMessage);
      } catch (RemoteException e) {
        e.printStackTrace();
      }
    }
  }

  @Override
  public IBinder onBind(Intent intent) {
    Toast.makeText(this, R.string.service_bound,
        Toast.LENGTH_SHORT).show();
    return messengerToClient.getBinder();
  }

  @Override
  public void onDestroy() {
    incomingHandler = null;
    Toast.makeText(this, R.string.service_destroyed,
        Toast.LENGTH_SHORT).show();
  }
}

public class ServiceConsumerActivity extends Activity
    implements OnClickListener {

  Messenger messengerToService = null;
  Messenger messengerFromService = new Messenger(
      new IncomingHandler());
  ServiceConnection connection =
      new MyServiceConnection();
  boolean isBound;

  void bind() {
    Intent intent = new Intent();
    intent.setAction("com.allmycode.WEATHER");
    isBound =
        bindService(intent, connection,
            Context.BIND_AUTO_CREATE);
  }

  void unbind() {
    if (isBound) {
      System.out.println("About to unbind");
      unbindService(connection);
      isBound = false;
    }
  }

  public void queryService() {
    Bundle bundle = new Bundle();
    bundle.putString("location", "Philadelpha");

    Message message = Message.obtain();
    message.replyTo = messengerFromService;
    message.setData(bundle);
    try {
      messengerToService.send(message);
    } catch (RemoteException e) {
      e.printStackTrace();
    }
  }

  class IncomingHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
      Bundle bundle = msg.getData();
      textView1.setText(bundle.getString("weather"));
    }
  }

  class MyServiceConnection implements ServiceConnection {
    public void onServiceConnected(
        ComponentName className, IBinder service) {

      messengerToService = new Messenger(service);

    }

    public void onServiceDisconnected(ComponentName n) {
      messengerToService = null;

      Toast.makeText(ServiceConsumerActivity.this,
          R.string.service_crashed, Toast.LENGTH_SHORT)
          .show();
    }
  }

  // //THIS STUFF IS BOILERPLATE:

  public static TextView textView1;
  Button bindButton, queryButton, unbindButton;
  EditText locationText;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    bindButton = (Button) findViewById(R.id.buttonBind);
    bindButton.setOnClickListener(this);
    locationText =
        (EditText) findViewById(R.id.editText1);
    queryButton = (Button) findViewById(R.id.buttonQuery);
    queryButton.setOnClickListener(this);
    textView1 = (TextView) findViewById(R.id.textView1);
    unbindButton =
        (Button) findViewById(R.id.buttonUnbind);
    unbindButton.setOnClickListener(this);
  }

  @Override
  public void onClick(View view) {
    if (view == bindButton) {
      bind();
    } else if (view == queryButton) {
      queryService();
    } else if (view == unbindButton) {
      unbind();
    }
  }
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to