android 6.0下獲取當前呼叫介面的包名

yingxian_Fei發表於2017-12-14

如下為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;


相關文章