Settings: android 组件如何响应语言变化
2012-09-13 17:07
393 查看
原文:http://blog.csdn.net/androidbluetooth/article/details/7182541
这里所说的 android 组件,主要是指 android 中 Activity、Service、ContentProvider 以及 BroadcastReceiver.
在 android 源码开发的过程中,大家拿到手的都是一样的 android 源码,但是硬件平台却是大相径庭,所以会引发各种各样的问题,于是乎,android 开发越发精彩!
这篇博客主要是在研究 Settings 源码时所激发的,把自己的经验拿出来分享一番!
我在设置语言之后,发现有些地方的语言还是没有改变,这个时候想起了 onConfigurationChanged 方法,先来看看这个方法。
public interface ComponentCallbacks
这个接口包括两个方法,其中一个就是onConfigurationChanged 方法。
Activity、Service、ContentProvider 都实现了这个接口,所以在代码中,我们可以重写这个方法,便于回调处理。那麽,这个方法何时才会被回调呢?
abstract voidonConfigurationChanged(Configuration newConfig)
Called by the system when the device configuration changes while your component is running.
设备配置发生变化的时候,就会回调。你可能实在憋不住要问,设备配置指哪些?
android:configChanges=["mcc", "mnc", "locale",
"touchscreen", "keyboard", "keyboardHidden",
"navigation", "screenLayout", "fontScale", "uiMode",
"orientation", "screenSize", "smallestScreenSize"]
这里需要提醒一下,如果使用 Activity 配合 onConfigurationChanged 方法,需要在其 menifest.xml 中添加:
android:configChanges 属性。
所以,如果你有需要可以在上面的三大组件中重写该方法,做你自己的逻辑处理!
如果,在 Settings 里面改变语言之后,在我们其它的 App 中可以注册某个广播就可以接收到这种变化,就更好了!
恩,当然可以!
注册一个广播:
[java] view
plaincopyprint?
<span style="font-size:18px;"><span style="font-family:'Comic Sans MS';">IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
registerReceiver(receiver, filter);</span></span>
接收广播:
[java] view
plaincopyprint?
<span style="font-size:18px;"><span style="font-family:'Comic Sans MS';">private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if("android.intent.action.CONFIGURATION_CHANGED".equals(action)) {
// to do
}
}
};</span></span>
别忘记在合适的位置取消注册:
unregisterReceiver(receiver);
这样做的话,要比直接重写onConfigurationChanged 方法,更加灵活,因为有些没有实现这个接口的android组件一大把,但是接收广播对于大多数组件来说还还是比较简单的!在以后的博客中,我和大家交流一下关于自定义的View如何接收广播!
关于 Intent.ACTION_CONFIGURATION_CHANGED,可以参考 sdk 文档。
有的人说,我现在只关心设备语言变化的那个 action,到底有木有?有!
Intent.ACTION_LOCALE_CHANGED
这里所说的 android 组件,主要是指 android 中 Activity、Service、ContentProvider 以及 BroadcastReceiver.
在 android 源码开发的过程中,大家拿到手的都是一样的 android 源码,但是硬件平台却是大相径庭,所以会引发各种各样的问题,于是乎,android 开发越发精彩!
这篇博客主要是在研究 Settings 源码时所激发的,把自己的经验拿出来分享一番!
我在设置语言之后,发现有些地方的语言还是没有改变,这个时候想起了 onConfigurationChanged 方法,先来看看这个方法。
public interface ComponentCallbacks
这个接口包括两个方法,其中一个就是onConfigurationChanged 方法。
Activity、Service、ContentProvider 都实现了这个接口,所以在代码中,我们可以重写这个方法,便于回调处理。那麽,这个方法何时才会被回调呢?
abstract voidonConfigurationChanged(Configuration newConfig)
Called by the system when the device configuration changes while your component is running.
设备配置发生变化的时候,就会回调。你可能实在憋不住要问,设备配置指哪些?
android:configChanges=["mcc", "mnc", "locale",
"touchscreen", "keyboard", "keyboardHidden",
"navigation", "screenLayout", "fontScale", "uiMode",
"orientation", "screenSize", "smallestScreenSize"]
这里需要提醒一下,如果使用 Activity 配合 onConfigurationChanged 方法,需要在其 menifest.xml 中添加:
android:configChanges 属性。
所以,如果你有需要可以在上面的三大组件中重写该方法,做你自己的逻辑处理!
如果,在 Settings 里面改变语言之后,在我们其它的 App 中可以注册某个广播就可以接收到这种变化,就更好了!
恩,当然可以!
注册一个广播:
[java] view
plaincopyprint?
<span style="font-size:18px;"><span style="font-family:'Comic Sans MS';">IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
registerReceiver(receiver, filter);</span></span>
接收广播:
[java] view
plaincopyprint?
<span style="font-size:18px;"><span style="font-family:'Comic Sans MS';">private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if("android.intent.action.CONFIGURATION_CHANGED".equals(action)) {
// to do
}
}
};</span></span>
别忘记在合适的位置取消注册:
unregisterReceiver(receiver);
这样做的话,要比直接重写onConfigurationChanged 方法,更加灵活,因为有些没有实现这个接口的android组件一大把,但是接收广播对于大多数组件来说还还是比较简单的!在以后的博客中,我和大家交流一下关于自定义的View如何接收广播!
关于 Intent.ACTION_CONFIGURATION_CHANGED,可以参考 sdk 文档。
有的人说,我现在只关心设备语言变化的那个 action,到底有木有?有!
Intent.ACTION_LOCALE_CHANGED
相关文章推荐
- Settings: android 组件如何响应语言变化
- Android如何让ListView的子组件按钮响应单击事件并修改该项子控件内容
- Android之隐式意图(Intent)如何查找匹配的组件
- 转 -android:程序无响应,你该如何定位问题?
- 如何看待 Kotlin 成为 Android 官方支持开发语言?
- Android中如何实现圆形按钮的颜色变化
- Android中如何检测重力感应变化:Sensor和SensorManager
- HTML---android webview组件如何使用 Webview与js交互
- 如何查看Android的ANR(无响应)异常
- Android四大组件之ContentProvider 全面解析,ContentResolver源码解析如何调用其它APP的ContentProvider
- c语言中如何处理变化参数问题
- Android中四大组件以及如何避免anr
- Android 四大组件 Service实现原理以及AIDL语言的使用详解
- 如何取得Android手机的当前语言设置
- Android 不同分辨率下如何使字体大小随之变化
- 如何创建一个依赖Android AAR文件的React Native组件
- 如何看待 Kotlin 成为 Android 官方支持开发语言?
- android_Activity响应Fragment内容变化
- 如何在Android中使用汇编语言
- android系统如何自适应屏幕大小 分类: Android界面和组件 2014-07-01 20:11 63人阅读 评论(0) 收藏