您的位置:首页 > 移动开发 > Android开发

Android中onConfigurationChanged未被调用的解决方法

2016-07-16 10:36 671 查看
程序如果需要监听系统设置的更改,则考虑重写Activity的onConfigurationChanged(Configuration newConfig)方法,这个方法是基于回调的事件处理方法:当系统设置为发生更改时,该方法会被自动触发。

但在写一个demo时,发现并没有触发该方法。

Java文件

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bn = (Button)findViewById(R.id.bn);
//为按钮绑定事件监听器
bn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Configuration config = getResources().getConfiguration();
//如果当前是横屏
if(config.orientation == Configuration.ORIENTATION_LANDSCAPE )
{
//设为竖屏
MainActivity.this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
);
}
//如果当前是竖屏
if(config.orientation == Configuration.ORIENTATION_PORTRAIT )
{
//设为横屏
MainActivity.this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE );
}
}
});
}
//重写该方法,用于监听系统设置的更改,主要是监控屏幕方向的更改
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
String screen = newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE
? "横向屏幕" : "竖向屏幕";
Toast.makeText(this , "系统的屏幕方向发生改变\n" +
"修改后的屏幕方向为:" + screen,Toast.LENGTH_LONG ).show();
}
}


xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/bn"
android:text="切换"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>


程序很简单,界面中仅仅由一个按钮,该按钮切换屏幕的方向,并提示用户屏幕方向被修改。实际情况中,点击该按钮,屏幕方向被切换了,却并没有提示信息。

这里要注意一点,要在AndroidMainfest.xml文件中设置

android:configChanges属性。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.com.suqian.configuration">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


Caution: Beginning with Android 3.2 (API level 13), the “screen size” also changes when the device switches between portrait and landscape orientation. Thus, if you want to prevent runtime restarts due to orientation change when developing for API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), you must include the “screenSize” value in addition to the “orientation” value. That is, you must decalare

android:configChanges=”orientation|screenSize”. However, if your application targets API level 12 or lower, then your activity always handles this configuration change itself (this configuration change does not restart your activity, even when running on an Android 3.2 or higher device).

意思就是说,从Android3.2(API 13)开始,screensize也随着设备的横竖而切换,所以,在AndroidManifest.xml里设置的MiniSdkVersion和TargetSdkVersion属性大于等于13的情况下,如果你想阻止程序在运行时重新加载Activity,除了设置”orientation“,你还必须设置”ScreenSize”。

例如:android:configChanges=”orientation|screenSize”,如果你的Target API 级别小于13,你的Activity自己会自动处理这种ScreenSize的变化。如果你的TargetSdkVersion小于13,即使你在Android 3.2或者更高级别的机器上运行程序,它还是会自己去处理ScreenSize的。

也就是说,解决方法就是在AndroidMainfest.xml的android:configChanges的属性改为orientation|screenSize

android:configChanges="orientation|screenSize">
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android java