android8.1从一个可以自由旋转的应用跳转到一个强制竖屏应用会闪横屏

       在系统相机开发过程中,从相机缩略图跳转到图库查看相片,再返回相机,相机会快速闪下横屏再切到竖屏。相机在AndroidMainifest.xml中已经配置了android:screenOrientation="portrait"(竖屏显示),图库是支持应用旋转的,正常情况下相机不应该闪横屏界面,此问题是android8.0的bug,修改如下:framework/base/services / core / java / com / android / server / wm / WindowManagerService.java

2596     /**
2597      * Determine the new desired orientation of the display, returning a non-null new Configuration
2598      * if it has changed from the current orientation.  IF TRUE IS RETURNED SOMEONE MUST CALL
2599      * {@link #setNewDisplayOverrideConfiguration(Configuration, int)} TO TELL THE WINDOW MANAGER IT
2600      * CAN UNFREEZE THE SCREEN.  This will typically be done for you if you call
2601      * {@link #sendNewConfiguration(int)}.
2602      *
2603      * The orientation is computed from non-application windows first. If none of the
2604      * non-application windows specify orientation, the orientation is computed from application
2605      * tokens.
2606      * @see android.view.IWindowManager#updateOrientationFromAppTokens(Configuration, IBinder, int)
2607      */
2608     boolean updateOrientationFromAppTokensLocked(boolean inTransaction, int displayId) {
2609         long ident = Binder.clearCallingIdentity();
2610         try {
2611             final DisplayContent dc = mRoot.getDisplayContent(displayId);
2612             final int req = dc.getOrientation();
2613                         /**modify-Determines whether the current application is portrait-zhoujy-181214 start*/
2614                         String screenOrientation = SystemProperties.get("screen_orientation_portrait", "false");
2615             if (req != dc.getLastOrientation() && "false".equals(screenOrientation)) {
2616                         /**modify-Determines whether the current application is portrait-zhoujy-181214 start*/
2617                 dc.setLastOrientation(req);
2618                 //send a message to Policy indicating orientation change to take
2619                 //action like disabling/enabling sensors etc.,
2620                 // TODO(multi-display): Implement policy for secondary displays.
2621                 if (dc.isDefaultDisplay) {
2622                     mPolicy.setCurrentOrientationLw(req);
2623                 }
2624                 if (dc.updateRotationUnchecked(inTransaction)) {
2625                     // changed
2626                     return true;
2627                 }
2628             }
2629
2630             return false;
2631         } finally {
2632             Binder.restoreCallingIdentity(ident);
2633         }
2634     }

修改完WindowManagerService.java之后再去相机的onStart中设置:  SystemProperties.set("screen_orientation_portrait", "true"),在onPause中设置SystemProperties.set("screen_orientation_portrait", "false")。修改完后此问题解决,当然最完美的解决方案是在系统中判断当前应用的AndroidMainifest.xml中是否设置了强制性横竖屏,需要继续优化。

你可能感兴趣的:(android)