when/how is binderDied() called?

1,948 views
Skip to first unread message

Joe Bryan

unread,
May 16, 2012, 5:54:55 PM5/16/12
to android-platform
Here's some simple client-process C++ that registers a callback that
is expected to be called when the service dies.
The binderDied() callback never gets called. When and how is it
supposed to be called?

class MyServiceClient : public IBinder::DeathRecipient
{
public:
MyServiceClient() { }

// DeathRecipient
virtual void binderDied(const android::wp<IBinder>& who);
};

void MyServiceClient::binderDied(const wp<IBinder>& who)
{
printf("server died!\n");
}

sp<MyServiceClient> sServiceClient;

int main(int argc, char *argv[])
{
// ...
binder = sm->getService(String16(MY_SERVICE_NAME));

if (sServiceClient == NULL)
sServiceClient = new MyServiceClient();

binder->linkToDeath(sServiceClient); // returns 0

// ... bunch of service IPC calls

sleep(10);

// ... service process is killed here with ctrl-C
// system log immediately reports: "I/ServiceManager( 109): service
<myservice> died"

binder->pingBinder(); // returns -32 when service is dead, 0
otherwise

// binderDied() never gets called
}

Joe Bryan

unread,
May 16, 2012, 9:47:06 PM5/16/12
to android-...@googlegroups.com
I needed to add this to the client process:

// we need a thread pool to receive binder callbacks (e.g., for DeathRecipient)
sp<ProcessState> proc(ProcessState::self());
ProcessState::self()->startThreadPool();

I assume this isn't necessary when the client is an actual android app.
Reply all
Reply to author
Forward
0 new messages