检索AIDL使用者的Java包名称(Retrieving Java package name of AIDL consumer)

编程入门 行业动态 更新时间:2024-10-25 22:35:13
检索AIDL使用者的Java包名称(Retrieving Java package name of AIDL consumer)

我通过AIDL机制暴露了一些API。 客户端需要绑定到AIDL并同步调用方法。 有没有办法可以检索客户端的Java包名?

例如,如果我将方法boolean isFooAvailable()公开为AIDL API,则从isFooAvalable的实现中,我可以确定绑定到AIDL服务的应用程序的Java包名称吗?

I am exposing some API through AIDL mechanism. Clients need to bind to the AIDL and call methods synchronously. Is there a way I can retrieve the client's Java package name?

For example, if I expose a method boolean isFooAvailable() as AIDL API, from within the implementation of isFooAvalable, can I determine the Java package name of the app that binds to the AIDL service?

最满意答案

是的,您可以在实现中找到包名称,如下所示:

IAidl.Stub mBinder = new IAidl.Stub() { @Override public boolean isFooAvailable() throws RemoteException { String pkgName = ""; int pid = Binder.getCallingPid(); ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> processes = am.getRunningAppProcesses(); for (ActivityManager.RunningAppProcessInfo proc : processes) { if (proc.pid == pid) { pkgName = proc.processName; } } // package name of calling application package Log.e("Package Name", pkgName); return false; } }

与UID相比,通过PID查找包名称是最佳方法。

Yes, you can find out the package name from within the implementation as follows :

IAidl.Stub mBinder = new IAidl.Stub() { @Override public boolean isFooAvailable() throws RemoteException { String pkgName = ""; int pid = Binder.getCallingPid(); ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> processes = am.getRunningAppProcesses(); for (ActivityManager.RunningAppProcessInfo proc : processes) { if (proc.pid == pid) { pkgName = proc.processName; } } // package name of calling application package Log.e("Package Name", pkgName); return false; } }

Finding package name through PID is the best approach compared to UID.

更多推荐

本文发布于:2023-08-03 18:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1396387.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:使用者   名称   Java   AIDL   package

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!