ACTION_CONFIGURATION_CHANGED || ACTION_LOCALE_CHANGED

本文讨论了在Android 1.6中关于ACTION_CONFIGURATION_CHANGED的bug,当系统语言改变时,某些UI未更新。在2.2版本中,新增ACTION_LOCALE_CHANGED广播用于监听语言变化。在旧版本中,ACTION_CONFIGURATION_CHANGED可能引发问题,而在2.2及更高版本,ACTION_LOCALE_CHANGED成为更合适的监听选项。对于活动,可以直接使用onConfigurationChanged()方法来响应语言变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

1.6calendar里,当系统语言改变时,calendar widget上的语言并没有随着改变。其实这个bugandroid的很多系统程序里都会出现。只要不是把string提取出来的,就都要响应“android.intent.action.CONFIGURATION_CHANGED”,为UI做一次update

这个bugHTCmagic上同样会出现。

 

1.6SDK里,“android.intent.action.CONFIGURATION_CHANGED”本来就是有bug的。网上居然没人讨论过这个问题。我们都知道,可以写一个broadcastReceiver来接受各种各样的Notification, 但是,假如你的broadcastReceiverintentfilter里允许了接收CONFIGURATION_CHANGED,那好,你的这个AP里面就不能再接收其它的通告,要不然,在开机的时候会出现错误提示:你的程序没响应!

不仅如此,假如我接收了ACTION_CONFIGURATION_CHANGED ,但是我还想知道,这到底是由语言改变引起的呢?还是屏幕旋转了?会不是在extra里给出信息?

我追了一下源码,在ActivityManagerService.java,有方法updateConfigurationLocked,这里是系统发送通告的地方:

 

 

                Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED);
                broadcastIntentLocked(null, null, intent, null, null, 0, null, null,
                        null, false, false, MY_PID, Process.SYSTEM_UID);

 

是的,我无法知道是怎样的设置改变。 

 

以上的讨论仅限于1.6,这一切在2.2SDK里不存在了!(1.62.2之间的SDK就没考证啦)

同时,在2.2里新加了一个通告:ACTION_CONFIGURATION_CHANGED,可以通过这个监听到系统语言改变。我也追了一下2.2的源码,在老地方: 

                Intent intent = new Intent(Intent.ACTION_CONFIGURATION_CHANGED);
                intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
                        | Intent.FLAG_RECEIVER_REPLACE_PENDING);
                broadcastIntentLocked(null, null, intent, null, null, 0, null, null,
                        null, false, false, MY_PID, Process.SYSTEM_UID);
                if ((changes&ActivityInfo.CONFIG_LOCALE) != 0) {
                    broadcastIntentLocked(null, null,
                            new Intent(Intent.ACTION_LOCALE_CHANGED),
                            null, null, 0, null, null,
                            null, false, false, MY_PID, Process.SYSTEM_UID);
                }

 

froyo里,当系统语言改变时,会发出两个系统通告,分别是CONFIGURATION_CHANGEDACTION_LOCALE_CHANGED。

 

还的明确一点变动:2.2的ACTION_CONFIGURATION_CHANGED只能通过registerReceiver()才会被接收,manifest里写进去的作废了。

 

总结:

如何监听系统语言改变?如果是在activity,直接可以用onConfigurationChanged();如果是旧的SDK,只能接收CONFIGURATION_CHANGED,并且这个通告很不好用!在2.2里,ACTION_LOCALE_CHANGED is enough!

转载于:https://www.cnblogs.com/ayiah/archive/2010/09/29/1838772.html

@Override protected void onAttachedToWindow() { super.onAttachedToWindow(); if (!mAttached) { mAttached = true; IntentFilter filter = new IntentFilter(); filter.addAction(Intent.ACTION_TIME_TICK); filter.addAction(Intent.ACTION_TIME_CHANGED); filter.addAction(Intent.ACTION_TIMEZONE_CHANGED); filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); filter.addAction(Intent.ACTION_USER_SWITCHED); getContext().registerReceiver(mIntentReceiver, filter, null, getHandler()); } // NOTE: It's safe to do these after registering the receiver since the receiver always runs // in the main thread, therefore the receiver can't run before this method returns. // The time zone may have changed while the receiver wasn't registered, so update the Time mCalendar = Calendar.getInstance(TimeZone.getDefault()); // Make sure we update to the current time postDelayed(new Runnable() { @Override public void run() { updateClock(); } }, 1000); } private final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(Intent.ACTION_TIMEZONE_CHANGED)) { String tz = intent.getStringExtra("time-zone"); mCalendar = Calendar.getInstance(TimeZone.getTimeZone(tz)); if (mClockFormat != null) { mClockFormat.setTimeZone(mCalendar.getTimeZone()); } } else if (action.equals(Intent.ACTION_CONFIGURATION_CHANGED)) { final Locale newLocale = getResources().getConfiguration().locale; if (! newLocale.equals(mLocale)) { mLocale = newLocale; mClockFormatString = ""; // force refresh } } updateClock(); } }; final void updateClock() { if (mDemoMode) return; mCalendar.setTimeInMillis(System.currentTimeMillis()); setText(getSmallTime()); }
06-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值