如下为android 6.0的framework中看到的获取调用应用app包名的方法,mark一下,直接上大致的代码:
String packageName = ActivityThread.currentOpPackageName();
if (packageName == null) {
// Package name can be null if the activity thread is running but the app
// hasn't bound yet. In this case we fall back to the first package in the
// current UID. This works for runtime permissions as permission state is
// per UID and permission realted app ops are updated for all UID packages.
String[] packageNames = ActivityThread.getPackageManager().getPackagesForUid(
android.os.Process.myUid());
if (packageNames != null && packageNames.length > 0) {
return packageNames[0];
}
}
return null;