Android 监听系统语言切换
方法一:在 Activity 中监听
1、在 AndroidManifest.xml 对应的 Activity 节点添加如下配置:
android:configChanges="locale|layoutDirection"
注意:locale 和 layoutDirection 都要添加
2、在 Activity 中添加如下代码:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
方法二:在 Application 中监听
这种情况,无须在 AndroidManifest.xml 中进行注册,可在 application 代码中直接重写即可:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// do something ...
}
方法三:使用广播监听
这里通过静态注册的方式进行
1、编写广播
public class LocaleChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "mReceiver onReceive intent.getAction(): "+intent.getAction());
if(intent.getAction().equals(Intent.ACTION_LOCALE_CHANGED)) {
Log.e("LocaleChangeReceiver","Language change");
// do something ...
}
}
}
2、在 AndroidManifest.xml 注册广播
<receiver android:name=".LocaleChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.LOCALE_CHANGED"/>
</intent-filter>
</receiver>