Sarfaraz
Sarfaraz

Reputation: 345

Which is the "Android.App.ActivityManagerNative" equivalent class for xamarin?

I want to change the device language through my xamarin native application. I am easily able to do this using the following code in the android studio.

public static void changeLanguage()
        throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException,
        IllegalArgumentException, InvocationTargetException, NoSuchFieldException {
    @SuppressWarnings("rawtypes")
    Class amnClass = Class.forName("android.app.ActivityManagerNative");
    Object amn = null;
    Configuration config = null;

    // amn = ActivityManagerNative.getDefault();
    Method methodGetDefault = amnClass.getMethod("getDefault");
    methodGetDefault.setAccessible(true);
    amn = methodGetDefault.invoke(amnClass);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        // getConfiguration moved from ActivityManagerNative to ActivityManagerProxy
        amnClass = Class.forName(amn.getClass().getName());
    }


    // config = amn.getConfiguration();

    Method methodGetConfiguration = amnClass.getMethod("getConfiguration");
    methodGetConfiguration.setAccessible(true);
    config = (Configuration) methodGetConfiguration.invoke(amn);

    // config.userSetLocale = true;
    @SuppressWarnings("rawtypes")
    Class configClass = config.getClass();
    Field f = configClass.getField("userSetLocale");
    f.setBoolean(config, true);

    Locale locale = new Locale("zh", "CN");

    // set the locale to the new value
    config.locale = locale;

    // amn.updateConfiguration(config);
    Method methodUpdateConfiguration = amnClass.getMethod("updateConfiguration", Configuration.class);
    methodUpdateConfiguration.setAccessible(true);
    methodUpdateConfiguration.invoke(amn, config);
}

I want to do the same through Xamarin. But in xamarin it throws the below error.

Caused by: java.lang.ClassNotFoundException: Didn't find class "Android.App.ActivityManagerNative"

Please let me know which class can I use in Xamarin for achieving the same?

Upvotes: 0

Views: 746

Answers (1)

Grace Feng
Grace Feng

Reputation: 16652

Can you please guide me where am I going wrong? Or can you please share your project with me?

Here is my code:

var amnClass = Java.Lang.Class.ForName("android.app.ActivityManagerNative");
Java.Lang.Object amn = null;
Configuration config = null;

var methodGetDefault = amnClass.GetMethod("getDefault");
methodGetDefault.Accessible = true;
amn = methodGetDefault.Invoke(amnClass);

if (Build.VERSION.SdkInt >= Build.VERSION_CODES.O)
{
    amnClass = Java.Lang.Class.ForName(amn.Class.Name);
}

var methodGetConfiguration = amnClass.GetMethod("getConfiguration");
methodGetConfiguration.Accessible = true;
config = methodGetConfiguration.Invoke(amn) as Configuration;

var configClass = config.Class;
var f = configClass.GetField("userSetLocale");
f.SetBoolean(config, true);

Locale locale = new Locale("zh", "CN");

config.Locale = locale;

var methodUpdateConfiguration = amnClass.GetMethod("updateConfiguration", Java.Lang.Class.FromType(typeof(Configuration)));
methodUpdateConfiguration.Accessible = true;
try
{
    methodUpdateConfiguration.Invoke(amn, config);
}
catch (InvocationTargetException ee)
{
    var error = ee.TargetException;
}

And here is my demo.

Upvotes: 2

Related Questions