现在的位置: 首页 > 综合 > 正文

应用放到android4.0终端,遇到了异常退出的问题.

2012年08月15日 ⁄ 综合 ⁄ 共 1260字 ⁄ 字号 评论关闭

【重现方法】

1. 在有些activity中横屏时,应用会异常退出。

 

【原因】

在这些activity的onCreate方法中使用了广播机制,

如:registerReceiver(m_oSDReceiver, getSdcardIntentFilter());

在横屏时会多次调用onCreate, 即多次注册了广播,导致死机。

 

【分析结果】

1. 如果你的sdk版本是2.3之前的,要做如下处理如下:

    1)在AndroidManifest.xml中,找到对应的activity添加: android:configChanges="orientation|keyboardHidden|locale

   2) 在activity 中重载onConfigurationChanged方法 

如果配置了android:configChanges这个属性,当我们横竖屏切换的时候会直接调用onCreate方法中的onConfigurationChanged方法,而不会重新执行onCreate方法,那当然如果不配置这个属性的话就会重新调用onCreate方法.

  public void onConfigurationChanged(Configuration newConfig){ 

   super.onConfigurationChanged(newConfig); 

    if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){ 

      //横向 

     }else{ 

      //竖向 

     }

    } 

2.  如果sdk是3.2以后的版本,需要添加 screenSize属性,如下:

    1) android:configChanges="orientation|keyboardHidden|locale|screenSize"

    2) 在activity 中重载onConfigurationChanged方法 

 

添加screenSize的原因,如下:

"screenSize" The current available screen size has changed. This represents a change in the currently available size, relative to the current aspect ratio, so will change when the user switches between landscape and portrait. 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).

Added in API level 13.

抱歉!评论已关闭.