androidserviceandroid-binderbindservice

How to get the client id which is trying to bind my service in Android


I have read thread in the below link. But this is too old and many said in various possiblities. How to get application package name or UID which is trying to bind my service from onBind function?

I have altered the question according to my need. I have a bound service in an application and two clients will bind to my service using bindservice. when applications are tried to bind this service I want to know which application is trying to bind my service in onBind function?

Is there any better possible ways in latest Android version to get the application name or UID which is trying to bind my service in onBind function?


Solution

  • Official android onBind method don't support you to know the caller because your service is intended to behave similarly for all clients. There are some workarounds you can try which may/may not work as the Android progress like below one.

     String callingApp = context.getPackageManager().getNameForUid(Binder.getCallingUid());
    

    If you want to provide service based on client then its good option to create two different services for the two clients if the clients are too many then this is not a good solution. Also you can protect your service by adding signature permission so that your service will be called by only the intended client.