You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为AOSP平板配置横竖屏对应的默认壁纸?

解决AOSP横竖屏切换时自动切换对应壁纸的方案

核心思路

AOSP默认壁纸机制会对单张壁纸进行旋转缩放,不区分横竖屏资源,因此需要修改系统壁纸服务WallpaperManagerService,监听屏幕方向变化并主动切换对应壁纸资源。

具体步骤

  1. 放置横竖屏壁纸资源
    将你的横屏、竖屏壁纸分别命名(如wallpaper_land.png、wallpaper_port.png),放入以下任一目录:

    • 系统框架资源目录:frameworks/base/core/res/res/drawable-xxhdpi/(根据分辨率选择对应dpi目录)
    • 设备定制overlay目录:device/[你的厂商名]/[你的设备型号]/overlay/frameworks/base/core/res/res/drawable-xxhdpi/(推荐用overlay,无需修改framework核心源码)
  2. 修改WallpaperManagerService代码
    打开frameworks/base/services/core/java/com/android/server/wallpaper/WallpaperManagerService.java,做以下修改:

    • 添加方向切换方法:

      private void switchWallpaperByOrientation(int orientation) {
          // 仅处理系统用户,多用户场景按需调整
          if (mCurrentUser != UserHandle.USER_SYSTEM) return;
      
          String resName = orientation == Configuration.ORIENTATION_LANDSCAPE 
              ? "wallpaper_land" 
              : "wallpaper_port";
          int resId = getResources().getIdentifier(resName, "drawable", "android");
          if (resId == 0) {
              Slog.w(TAG, "Orientation wallpaper resource not found: " + resName);
              return;
          }
      
          try (ParcelFileDescriptor pfd = getResources().openRawResourceFd(resId).getParcelFileDescriptor()) {
              setWallpaper(pfd, null, true, UserHandle.USER_SYSTEM);
          } catch (IOException e) {
              Slog.e(TAG, "Failed to set orientation-specific wallpaper", e);
          }
      }
      
    • 注册配置变化广播接收器:
      在类中添加广播接收器实例:

      private final BroadcastReceiver mConfigChangeReceiver = new BroadcastReceiver() {
          @Override
          public void onReceive(Context context, Intent intent) {
              if (!Intent.ACTION_CONFIGURATION_CHANGED.equals(intent.getAction())) return;
      
              Configuration newConfig = intent.getParcelableExtra(Intent.EXTRA_CONFIGURATION);
              if (newConfig == null) return;
      
              // 避免重复切换,仅当方向实际变化时触发
              if (mLastOrientation != newConfig.orientation) {
                  mLastOrientation = newConfig.orientation;
                  switchWallpaperByOrientation(newConfig.orientation);
              }
          }
      };
      

      同时在类中添加成员变量记录上次方向:

      private int mLastOrientation = Configuration.ORIENTATION_UNDEFINED;
      
    • 在服务启动时注册广播:
      在onCreate()方法末尾添加:

      IntentFilter filter = new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED);
      registerReceiver(mConfigChangeReceiver, filter);
      
  3. 编译验证
    重新编译AOSP镜像,刷入设备后,切换横竖屏即可看到壁纸自动切换。

注意事项

  • 如果使用overlay方式添加资源,需确保overlay模块已正确配置到设备的编译脚本中。
  • 多用户场景下,需调整switchWallpaperByOrientation方法中的用户判断逻辑,支持切换不同用户的壁纸。
  • 若设备使用自定义Launcher(而非AOSP默认Launcher3),需确认Launcher是否会覆盖壁纸显示逻辑,必要时同步修改Launcher代码。

内容的提问来源于stack exchange,提问作者BryanB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 13:35:58