Android菜鸟之学习android源码之三(修改系统默认横屏)

好多小伙伴在开发平板产品的时候都会接到把系统默认竖屏改成默认横屏的任务,网上查了许多的文章,有说改底层的c++文件的,有说改系统编译出来的一些配置文件的,众说纷纭,我当初也踩过了不少的坑,最终发现一种比较靠谱的方式,那就是修改WindowManagerService,这个类是位于framework里的,修改后需要对整个framework进行模块编译后替换掉系统的framework.jar这个包,这个类的具体路径是

platform\frameworks\base\services\core\java\com\android\server\wm\WindowManagerService.class
具体改动的地方看代码吧,

   public int getOrientationFromWindowsLocked() {
        ....
        -- return (mLastWindowForcedOrientation=ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
        ++ return (mLastWindowForcedOrientation=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
  /*
     * Determine the new desired orientation of the display, returning
     * a non-null new Configuration if it has changed from the current
     * orientation.  IF TRUE IS RETURNED SOMEONE MUST CALL
     * setNewConfiguration() TO TELL THE WINDOW MANAGER IT CAN UNFREEZE THE
     * SCREEN.  This will typically be done for you if you call
     * sendNewConfiguration().
     *
     * The orientation is computed from non-application windows first. If none of
     * the non-application windows specify orientation, the orientation is computed from
     * application tokens.
     * @see android.view.IWindowManager#updateOrientationFromAppTokens(
     * android.os.IBinder)
     */
    boolean updateOrientationFromAppTokensLocked(boolean inTransaction) {
        long ident = Binder.clearCallingIdentity();
        try {
            int req = getOrientationFromWindowsLocked();
            if (req == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
-- mForcedAppOrientation = req;
-- req = getOrientationFromAppTokensLocked();
         ++req=ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
            }

            if (req != mForcedAppOrientation) {
                -- mForcedAppOrientation = req;
                ++ mForcedAppOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
....
    }
 boolean computeScreenConfigurationLocked(Configuration config) {

.... if (config != null) { -- config.orientation = (dw <= dh) ? Configuration.ORIENTATION_PORTRAIT:Configuration.ORIENTATION_LANDSCAPE; ++ config.orientation = Configuration.ORIENTATION_LANDSCAPE; } ....
 return true;
 }

修改这几处代码便能把系统默认的竖屏变成横屏,但是会有其他的应用出现问题,比如拨号盘原本竖屏显示的,但是在这种横屏模式下打开拨号盘会报空指针错误,进源码一看原来拨号盘也准备了两套的布局,一套横屏一套竖屏的,单独修改windowmanagerservice后不知道为什么这些系统应用没有变成竖屏模式,或许是时间仓促,并没有对windowmanagerservice的流程作很详细的分析,以后有时间再找机会研究下这个流程吧。

    原文作者:小剑温暖
    原文地址: https://blog.csdn.net/baidu_20792589/article/details/54708621
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞