In the onBind()
method of a service I'd like to check whether the caller has a particular permission. For that I need to find the identity of the caller.
I expected the following code to return the caller package name but instead I get the package name of the service. What am I doing wrong?
Binder.getCallingUid()
String pkg = getPackageManager().getNameForUid(uid);
There's no need to do this type of operation. If you want to protect binding, starting or stopping your
Service
then add theandroid:permission
attribute to your manifest declaration of theService
:The system will automatically ensure that any app trying to use your service holds the specified permission. You only use the enforce/check permission APIs inside of binder exposed calls. You may find this article helpful: http://po.st/d6eTXj.