android - 如何获取试图从 onBind 函数绑定(bind)我的服务的应用程序包名称或 UID?

标签 android

我在一个应用程序中有一个服务,我可以从不同的应用程序访问这个服务。 当应用程序尝试绑定(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/

相关文章:

android - 当数据发生变化时,如何从我的 firebase 存储中获取数据?安卓

android - 即使在加载 View 后,我的 ProgressDialog 也不会关闭。

android - Xamarin Android 异常导致 "Frame not in module"

android - 无法将 JSON 解析的数据加载到 ArrayList 中并显示在 ListView 上

安卓 : RecyclerView onclick update another RecyclerView

java - 将字符串从线程蓝牙服务发送到主 Activity 中的 textView

android - 如何从 Android 中的 C++ 库调用方法?

android - 有没有办法参数化动画?

android - 更改 ActionBar 中的操作溢出文本颜色

android - android 中的 inmobi 广告出现问题