我在一个应用程序中有一个服务,我可以从不同的应用程序访问这个服务。 当应用程序尝试绑定(bind)此服务时,我想知道哪个应用程序试图在 onBind 函数中绑定(bind)我的服务, 但是我在 onBind 函数中无法获取此应用程序的包名或 UID。
是否可以在 onBind 函数中获取尝试绑定(bind)我的服务的应用程序名称或 UID?
最佳答案
您可以使用以下方法来确定调用应用程序。
String callingApp = context.getPackageManager().getNameForUid(Binder.getCallingUid());
请务必注意 JavaDoc for getCallingUid()
其中说:
Return the Linux uid assigned to the process that sent you the current transaction that is being processed. This uid can be used with higher-level system services to determine its identity and check permissions. If the current thread is not currently executing an incoming transaction, then its own uid is returned.
关于android - 如何获取试图从 onBind 函数绑定(bind)我的服务的应用程序包名称或 UID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12918731/