String str = Locale.getDefault().getLanguage();
Log.i("my", str);
中文为 str ="cn";
英文为 str ="en";
日文为 str ="ja";
。。。。
写个程序监控设置中语言变化
public class AndroidLanguageActivity extends Activity { class MyReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if("Intent.ACTION_LOCALE_CHANGED".equals(intent.getAction())) { String str = Locale.getDefault().getLanguage(); Log.i("my", "changed "+str); } } } MyReceiver receiver; IntentFilter filter; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); View view = new TestDrawView(this); // view.setBackgroundDrawable(getResources().getDrawable(R.drawable.xx)); setContentView(view); receiver = new MyReceiver(); filter = new IntentFilter("Intent.ACTION_LOCALE_CHANGED"); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); registerReceiver(receiver, filter); String str = Locale.getDefault().getLanguage(); Log.i("my", str); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); unregisterReceiver(receiver); } }
根据官方文档指明,注册广播在onSume()中,解除注册unregister(receiver);在onPause()中。
而且如果假设广播只在本应用中的话推荐使用LocalBroadcastManager,因为它具有一下优点:
1 因广播数据在本应用范围内传播,你不用担心隐私数据泄露的问题。
2 不用担心别的应用伪造广播,造成安全隐患。
3 相比在系统内发送全局广播,它更高效。