问题描述
我揭露过机制的一些API。客户端需要绑定到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?
例如,如果我公开的方法布尔isFooAvailable()
作为AIDL API,从isFooAvalable的实现中,我能确定该应用程序的Java包名称绑定到AIDL服务?
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?
推荐答案
是的,你可以在执行中找出包名称如下:
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;
}
}
通过PID查找包的名称是最好的办法相比,UID。
Finding package name through PID is the best approach compared to UID.
这篇关于AIDL消费者获取Java包名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!