保持方向更改以实现完整设备颤动 Android 11

发布于 2025-01-12 00:41:19 字数 907 浏览 4 评论 0原文

使用以下代码,我可以更改应用程序中设备的方向

void rotate(orientation) {
    if (!_isEnabled) return;

    var property = DeviceOrientation.landscapeRight;

    switch (orientation) {
      case "Portrait":
        property = DeviceOrientation.portraitUp;
        break;
      case "Reverse Portrait":
        property = DeviceOrientation.portraitDown;
        break;
      case "Landscape Left":
        property = DeviceOrientation.landscapeLeft;
        break;
      case "Landscape Right":
        property = DeviceOrientation.landscapeRight;
        break;
    }

    setState(() {
      SystemChrome.setPreferredOrientations([property]);
    });
  }

,但是如果我退出应用程序并切换到其他应用程序,方向将更改为默认纵向。

即使按主页按钮也会默认方向。

我试图保持整个设备的方向固定。

我发现 Google Play 商店上的一个应用程序在 Android 10 之前都可以工作。从 Android 11 开始,它就无法工作了。

我使用的是搭载 Android 11 和 MIUI 12.5.5 的小米红米 9 Power,

如何保持整个设备的方向固定?

Using the following code I'm able to change the orientation of the device in app

void rotate(orientation) {
    if (!_isEnabled) return;

    var property = DeviceOrientation.landscapeRight;

    switch (orientation) {
      case "Portrait":
        property = DeviceOrientation.portraitUp;
        break;
      case "Reverse Portrait":
        property = DeviceOrientation.portraitDown;
        break;
      case "Landscape Left":
        property = DeviceOrientation.landscapeLeft;
        break;
      case "Landscape Right":
        property = DeviceOrientation.landscapeRight;
        break;
    }

    setState(() {
      SystemChrome.setPreferredOrientations([property]);
    });
  }

However if I exit the app and switch to some other app the orientation changes to default portrait.

Even pressing the home button defaults the orientation.

I'm trying to keep the orientation fixed for the whole device.

I found an app on Google Play Store works till Android 10. From Android 11 it doesn't work.

I'm using a Xiaomi Redmi 9 Power with Android 11 and MIUI 12.5.5

How can I keep the orientation fixed for whole device?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏花。依旧 2025-01-19 00:41:19

你不能也不应该。 Android 已经有系统级方向配置,用户可以选择固定方向。当您的应用程序进入后台时,您的特权就会终止。其他一些应用程序想要水平播放视频,所以它会是这样,而你的应用程序与它无关。启动器通常仅支持纵向,并且不会横向显示,因为它不方便用户使用。

you can't and you shouldn't. Android already has a system-level orientation configuration and user may opt-in to fixed orientation. Your privileges ends when your app goes to background. Some other app want to play video in horizontal so it will be and yours has nothing to do with it. Launchers are oftenly supporting only portrait and won't ever be shown in horizontal, as its not user-friendly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文