如何使用DisplayManager获取显示器并设置布尔值。

huangapple go评论72阅读模式
英文:

How to: Use DisplayManager to get displays and set a bool

问题

我的 Java 理解基本上是不存在的.. 我只真正理解 Dart

然而我需要使用 `DisplayManager``getDisplays()` 来设置一个布尔值

我需要类似这样的代码我知道这实际上不是真正的 Java只是为了让别人理解):

    DisplayManager displayManager = DisplayManager();
    List<Display> displays = [];
    boolean hasDisplays = false;
    
    void _getDisplays(){
    displays = DisplayManager.getDisplays();
    }
    
    void _displayBool(){
    if(displays.length > 1){
    hasDisplays = true;
    } else {
    hasDisplays = false;
    }
}

如果有人可以向我展示在这种情况下该如何做类似的事情,那将是很棒的。

public class MainActivity extends FlutterActivity {
    
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  }
}
英文:

My understanding of Java is basically nonexistent .. I only really understand Dart.

However, I need to use DisplayManager and getDisplays() in order to set a boolean.

I need something like this (I know this isn't really java, it's just so someone can get an understanding):

DisplayManager displayManager = DisplayManager();
List&lt;Display&gt; displays = [];
bool hasDisplays = false;

void _getDisplays(){
displays = DisplayManager.getDisplays();
}

void _displayBool(){
if(displays.length &gt; 1){
hasDisplays = true;
} else {
hasDisplays = false;
}

if someone could show me how the hell I'm supposed to do something like that, inside of this, it would be amazing

public class MainActivity extends FlutterActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  }
}

答案1

得分: 1

您需要使用context.getSystemService()函数获取DisplayManager。然后您可以使用displayManager.getDisplays()函数获取显示器列表(Display[])。如果您想要知道列表中是否有一个或多个显示器,则将语句设置为布尔值。

DisplayManager displayManager = (DisplayManager) getApplicationContext().getSystemService(Context.DISPLAY_SERVICE);
Display[] displays = displayManager.getDisplays();
boolean hasDisplays = displays.length >= 1;
英文:

You need to get the DisplayManager with the context.getSystemService() function. Then you get the list of Displays (Display[]) from the displayManager.getDisplays() function. Then if you want to know if there are one or more displays in the list, set the statement to the boolean.

   DisplayManager displayManager = (DisplayManager) getApplicationContext().getSystemService(Context.DISPLAY_SERVICE);
   Display[] displays = displayManager.getDisplays();
   boolean hasDisplays = displays.length &gt;= 1;

答案2

得分: 0

好的,以下是您要翻译的代码部分:

class TestScreenCount {
  public static void main(String[] args) {
    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] allScreens = env.getScreenDevices();
    System.out.println(Arrays.toString(allScreens));
  }
}

这段代码会输出已连接的显示器,以我的情况为例(我有一个外部显示器和一个笔记本电脑屏幕):

[X11GraphicsDevice[screen=0], X11GraphicsDevice[screen=1]]
英文:

Well you can try this code:

class TestScreenCount {
  public static void main(String[] args) {
    GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
    GraphicsDevice[] allScreens = env.getScreenDevices();
    System.out.println(Arrays.toString(allScreens));
  }
}

This will output the connected displays, for my case (I have an external screen and a laptop screen):

[X11GraphicsDevice[screen=0], X11GraphicsDevice[screen=1]]

huangapple
  • 本文由 发表于 2020年10月6日 18:19:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64223814.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定