Service 如何知道caller

时间:2023-03-09 18:06:10
Service 如何知道caller

重写Binder的onTransact方法

1  
you need to do that in Binder#onTransact method, this is a good place for any remote checks – pskink Feb 10 at 17:58
    
The description of Binder#onTransact mentions unmarshalling transactions. Is it safe to use it just for checking permissions and returning false when the permissions are not okay, and returning the default implementation when all is well? – Daniel Feb 11 at 15:36
    
i think so, but the docs do not say what is returned boolean for (try to return false and see what happens) or you would try to throw RemoteException saying "dear client, nonono, you have no permissions to call me" – pskink Feb 11 at 16:03
    
I did try it. When I return false from onTransact, the function call on the client returns null. – Daniel
拿到caller的包名
String pkg = getPackageManager().getNameForUid(getCallingUid());