Issue
I want my app to be aware anytime the user changes the locale. So in my Application
class, I create a receiver
to listen to the system intent ACTION_LOCALE_CHANGED
:
public final class MyApp extends Application {
private BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override public void onReceive(Context context, Intent intent) {
String locale = Locale.getDefault().getCountry();
Toast.makeText(context, "LOCALE CHANGED to " + locale, Toast.LENGTH_LONG).show();
}
};
@Override public void onCreate() {
IntentFilter filter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED);
LocalBroadcastManager.getInstance(this).registerReceiver(myReceiver, filter);
}
}
When I press home and go to the settings app to change my locale, the Toast is not shown. Setting the breakpoint inside onReceive
shows it never gets hit.
Solution
Intent.ACTION_LOCALE_CHANGED
is not a local broadcast, so it won't work when you register it with LocalBroadcastManager
. LocalBroadcastManager
is used for the broadcast used inside your app.
public class MyApp extends Application {
private BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String locale = Locale.getDefault().getCountry();
Toast.makeText(context, "LOCALE CHANGED to " + locale,
Toast.LENGTH_LONG).show();
}
};
@Override
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter(Intent.ACTION_LOCALE_CHANGED);
registerReceiver(myReceiver, filter);
}
}
Answered By - alijandro
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.