Android屏幕旋转和Configuration的使用演示
转自:http://blog.csdn.net/lfdfhl/
MainActivity如下:
package cn.testconfigurationchange; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.app.Activity; import android.content.res.Configuration; /** * Demo描述: * 测试Configuration和屏幕旋转 * * 测试步骤: * 1 点击Button利用Configuration查看手机上的配置信息 * 2 在manifest为Activity配置 android:configChanges="orientation" * 由于设备和系统版本的差异,少数情况下该设置无效,建议设置为: * android:configChanges="keyboardHidden|orientation|screenSize" * 表示当前Activity可以对屏幕是否旋转进行监听(当然也可对其他系统信息进行监听) * 配置后屏幕旋转时会调用onConfigurationChanged()方法. * 若不配置,那么每次屏幕旋转的时候都会调用Activity的onCreate()方法 * 而不会调用onConfigurationChanged() * 3 取消在2中的设置.此时每次旋转屏幕都会调用onCreate(),并在屏幕旋转前 * 调用onSaveInstanceState()保存现场状态,在选中后调用、 * onRestoreInstanceState()恢复现场. * 所以此时调用顺序为: * onSaveInstanceState->onCreate()->onRestoreInstanceState() * 除此以外:当系统内存紧张时可暂时杀死该Activity,内存允许时重启 * 该Activity.在这样情况下也是该调用顺序,原理亦类似. * * 备注说明: * 该Demo的测试为1和2.若要测试3去掉manifest为Activity配置的 * android:configChanges="orientation" 即可 * */ public class MainActivity extends Activity { private Button mButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); System.out.println("---> onCreate()"); init(); } private void init(){ mButton=(Button) findViewById(R.id.button); mButton.setOnClickListener(new ClickListenerImpl()); } private class ClickListenerImpl implements OnClickListener{ @Override public void onClick(View v) { getConfigurationInfo(); } } private void getConfigurationInfo(){ Configuration configuration=getResources().getConfiguration(); //获取屏幕方向 int l=configuration.ORIENTATION_LANDSCAPE; int p=configuration.ORIENTATION_PORTRAIT; if (configuration.orientation==l) { System.out.println("现在是横屏"); } if (configuration.orientation==p) { System.out.println("现在是竖屏"); } //获取国家码和网络码 int countryNum=configuration.mcc; int netNum=configuration.mnc; System.out.println("国家码="+countryNum+",网络码="+netNum); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); System.out.println("---> onConfigurationChanged()"); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("name", "zxx"); outState.putInt("id", 9527); System.out.println("---> onSaveInstanceState()"); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); String name=savedInstanceState.getString("name"); int id=savedInstanceState.getInt("id"); System.out.println("---> onRestoreInstanceState()"); System.out.println("名字="+name+",编号="+id); } }
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="获取Configuration信息" android:textSize="25sp" android:layout_marginTop="80dip" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="测试ConfigurationChange" android:textSize="25sp" android:layout_centerInParent="true" /> </RelativeLayout>
AndroidManifest.xml如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.testconfigurationchange" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="cn.testconfigurationchange.MainActivity" android:configChanges="orientation" 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>