最近在项目开发中遇到一个很奇怪的问题,就是在语言切换后有的apk会黑屏且这个时候不在响应操作。看现象感觉是apk卡死或者挂掉了,但是通过ps查看应用的进程是正常运行的。后来就自己写了一个apk专门切换语言,下面是完成的过程写出来供大家参考。
public class MainActivity extends Activity {
private Bundle bundle = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bundle = savedInstanceState;
setContentView(R.layout.activity_main);
Log.d("wdp_test", "oncreate----->");
new Thread()
{
public void run()
{
try {
Thread.sleep(3000);
setlanguage();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
Log.d("wdp_test", "onConfigurationChanged----->");
this.onCreate(bundle);
//Toast.makeText(this, "onConfigurationChanged", Toast.LENGTH_LONG).show();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d("wdp_test", "onDestory---->");
//finish();
}
private void setlanguage(){
// TODO Auto-generated method stub
//Toast.makeText(this, "chang lang", Toast.LENGTH_LONG).show();
IActivityManager am = ActivityManagerNative.getDefault();
try {
Configuration config = am.getConfiguration();
Locale loc =null;// mLocales[position];
Log.d("wdp_test", "now language is "+Locale.getDefault().getLanguage());
if(Locale.getDefault().getLanguage().contains("zh")) //获得当前语言并判断是否是中文
config.locale = Locale.ENGLISH; //Locale类中定义了各种语言
}else
{
config.locale = Locale.CHINA;
}
am.updateConfiguration(config);
BackupManager.dataChanged("com.android.providers.settings");
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
上面是我实现的很简单的apk用来不断的在中文和英文之间切换,现在就来分析一下这段代码,因为要不断的切换语言所以在oncreat中就开了一个线程每隔3秒中就调用一次设置语言的函数setlanguage,这个函数是用来实现语言切换的,如上面红色字体的一段,是android标准的设置语言的方法这里就不在多说了。完成了上面的一段之后我们还需要在androimanifest.xml中添加一些属性,直接贴代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.languagetest"
android:sharedUserId="android.uid.system"
android:versionCode="1"
android:versionName="1.0"
android:configChanges="locale|layoutDirection">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.languagetest.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
因为我们的程序中需要改变语言就要在manifest中添加相应的权限,android:configChanges="locale|layoutDirection"和 <uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/>
添加了这样的连个配置我们的程序就会在系统config发生变化时监听这个消息,也有了android.permission.CHANGE_CONFIGURATION
的权限,这个时候导出apk在平台上运行,就会包下面的错误
E/AndroidRuntime( 1653): FATAL EXCEPTION: Thread-82
E/AndroidRuntime( 1653): Process: com.example.languagetest, PID: 1653
E/AndroidRuntime( 1653): java.lang.SecurityException:
Permission Denial: updateConfiguration() from pid=1653, uid=10019 requires android.permission.CHANGE_CONFIGURATION
permission denial就是没有权限,可是manifest中明明已经添加了这个权限了,这是为什么呢,经过很多的分析,很可能是因为我们需要system的权限,所以在manifest中添加属性android:sharedUserId="android.uid.system" 这个属性可以让我们运行在system进程中拥有sytstem的权限,然后再pm install 安装程序的时候报错Failure [INSTALL_FAILED_SHARED_USER_INCOMPATIBLE] ,参考了http://blog.youkuaiyun.com/mypotatolove/article/details/44155767和http://www.apkbus.com/android-152345-1-1.html两篇文章添加这个属性后需要有系统签名,所以把源码放到了android整个代码下添加android.mk后编译,这样生成的apk就有了系统签名。
再次运行就ok了,这样就完成了在自己的apk中实现语言的立即切换。