Vinícius Maia
Vinícius Maia

Reputation: 57

How to create a BroadcastReceiver for Locale in Xamarin?

I'm trying create a BroadcastReceiver to listen Locale changes, but I always have the java ClassNotFoundException.

That's my Locale BroadcastReceiver:

namespace AppDemia.Droid.Receivers
{
    [BroadcastReceiver(Enabled = true)]
    [IntentFilter(new[] { Intent.ActionLocaleChanged })]
    public class LocaleChangeReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            Mensagens.LanguageSystem = Locale.Default.Language; // Esse método pega a string simples (exemplo: "pt" ou "en"
            //Mensagens.languageSystem = Locale.Default.ToString();  // Esse aqui pega a string completa (exemplo: "pt_BR" ou "en_US")
            Mensagens.CultureSystem = System.Globalization.CultureInfo.CurrentUICulture.ToString(); // Aqui pegamos a string completa mas sem ser padrão android (exemplo: "pt-BR" ou "en-US", note o hifen)
            CultureInfo cultureSystem = new CultureInfo(System.Globalization.CultureInfo.CurrentUICulture.ToString());
            Mensagens.DateFormatSystem = cultureSystem.DateTimeFormat.ShortDatePattern;
        }
    }
}

And that's my Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="4" android:versionName="1.1.1" android:installLocation="auto" package="com.prosperi.appdemia">
    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="22" />
    <application android:label="Appdemia" android:theme="@style/Appdemia" android:icon="@drawable/LogoAppdemia">
        <!-- Other things -->

        <receiver android:name="com.prosperi.appdemia.receivers.LocaleChangeReceiver">
            <intent-filter>
                <action android:name="android.intent.action.LOCALE_CHANGED"></action>    
            </intent-filter>
        </receiver>
    </application>

    <!--other things -->
</manifest>

Always that I change the locale in the device settings, with my app opened, I have this exception: Unable to instantiate receiver com.prosperi.appdemia.receivers.LocaleChangeReceiver: java.lang.ClassNotFoundException: Didn't find class "com.prosperi.appdemia.receivers.LocaleChangeReceiver"

Can anyone help me?

Upvotes: 1

Views: 469

Answers (1)

Cheesebaron
Cheesebaron

Reputation: 24470

You don't need to manually add your receiver to your AndroidManifest.xml. It is done automatically as soon as you add the [BroadcastReceiver] attribute to your class.

Upvotes: 1

Related Questions