Frage

The stock Cyanogenmod ROM has support for profiles baked in and although I'm not sure if this is a part of the default Android functionality, I was wondering if it is possible to get the name of the currently selected profile.

I haven't been able to find any developer documentation on this.

(Assuming that the Android SDK doesn't support this, can a superuser app implement this functionality?)

Thanks


Trudging through some CM source I found the source code for the ProfileManager. The methods are public so I guess I don't need to go down the rabbit-hole of Java reflection...but in order to use these classes, I need some Cyanogenmod JARs to build against.

War es hilfreich?

Lösung

Got it. A bit of ugly reflection and voila. The classes are ProfileManager and Profile

    Object o = getSystemService("profile");
    try {

        Class<?> ProfileManager = Class.forName("android.app.ProfileManager");
        Class<?> Profile = Class.forName("android.app.Profile");
        try {

            Method getActiveProfile = ProfileManager.getDeclaredMethod("getActiveProfile", null);
            Method getName = Profile.getDeclaredMethod("getName", null);

            try {

                String strProfile = (String) getName.invoke(getActiveProfile.invoke(o));
                System.out.println("Current profile is: " + strProfile);

            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }           

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top