Пример #1
0
          public void onServiceConnected(ComponentName className, IBinder service) {
            // This is called when the connection with the service has been
            // established, giving us the service object we can use to
            // interact with the service.  We are communicating with our
            // service through an IDL interface, so get a client-side
            // representation of that from the raw service object.
            mService = IRemoteService.Stub.asInterface(service);
            mKillButton.setEnabled(true);
            mCallbackText.setText("Attached.");

            // We want to monitor the service for as long as we are
            // connected to it.
            try {
              mService.registerCallback(mCallback);
            } catch (RemoteException e) {
              // In this case the service has crashed before we could even
              // do anything with it; we can count on soon being
              // disconnected (and then reconnected if it can be restarted)
              // so there is no need to do anything here.
            }

            // As part of the sample, tell the user what happened.
            Toast.makeText(Binding.this, R.string.remote_service_connected, Toast.LENGTH_SHORT)
                .show();
          }
Пример #2
0
          public void onClick(View v) {
            if (mIsBound) {
              // If we have received the service, and hence registered with
              // it, then now is the time to unregister.
              if (mService != null) {
                try {
                  mService.unregisterCallback(mCallback);
                } catch (RemoteException e) {
                  // There is nothing special we need to do if the service
                  // has crashed.
                }
              }

              // Detach our existing connection.
              unbindService(mConnection);
              unbindService(mSecondaryConnection);
              unbindService(mThirdConnection);
              mKillButton.setEnabled(false);
              mIsBound = false;
              mCallbackText.setText("Unbinding.");
            }
          }