首页 > 代码库 > Android 锁屏生命周期问题

Android 锁屏生命周期问题

今天在做视频播放时碰到了一个问题:当用户按下电源键锁屏后(屏幕黑了)视频播放停止了。但是当按下解锁键后(屏幕亮,但是未解锁)的时候视频开始自动播放了。

 

我的Activity在manifest.xml 配置如下

android:configChanges="locale|orientation|keyboardHidden"

android:screenOrientation="landscape"


解决办法:

需要在android:configChanges="locale|orientation|keyboardHidden" 中添加一个screenSize 

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

 

谷歌官方说明:

The physical screen size has changed. This represents a change in size regardless of orientation, so will only change when the actual physical screen size has changed such as switching to an external display. A change to this configuration corresponds to a change in the smallestWidth configuration. 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).

 

主要是说,当你的程序目标API小于等于12时,切换显示就会重启你的Activity。

 

Android 锁屏生命周期问题