displaymanager.getdisplays()获取不到副屏需要权限吗
时间: 2024-01-14 20:00:46 浏览: 33
`DisplayManager.getDisplays()`是一个用于获取显示设备信息的方法,一般情况下不需要特殊的权限。
在Android中,`DisplayManager.getDisplays()`可以用来获取当前连接的显示设备信息,包括主屏和副屏等。这个方法并不需要特殊的权限,只要应用具有运行在Android设备上的基本权限即可调用该方法。
然而,有时候获取副屏的显示设备信息可能会受到系统设置或者设备制造商的限制,导致无法完全获取到副屏的信息。这并不是权限问题,而是设备本身的限制。在一些特殊的情况下,可能需要特定的系统权限或者设备厂商的授权才能完全访问到副屏的信息。
因此,如果`DisplayManager.getDisplays()`无法获取到副屏的信息,首先需要确保设备连接的副屏是正常工作的,并且在系统设置中启用了副屏幕。如果副屏完全正常工作但依然无法获取到信息,可能需要进一步了解系统设置和设备的限制,并且可能需要特定的系统权限或设备授权才能解决该问题。
相关问题
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.content.Context; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DisplayManager displayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE); Display[] displays = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION); if (displays.length > 0) { MyPresentation presentation = new MyPresentation(this, displays[0]); presentation.show(); } } } DisplayManager都是红色的
这个报错是因为 `DisplayManager` 类没有被导入,你可以在文件头部加上下面这行代码导入 `DisplayManager` 类:
```java
import android.hardware.display.DisplayManager;
```
如果还有其他的错误或者疑问,欢迎再次提问。
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.app.Presentation; import android.content.Context; import android.hardware.display.DisplayManager; import android.os.Bundle; import android.util.Log; import android.view.Display; import android.widget.TextView; // 继承自 Presentation 类,用于显示在外部显示设备上的内容 class MyPresentation extends Presentation { private TextView mText; public MyPresentation(Context context, Display display) { super(context, display); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 为这个 Presentation 设置布局 setContentView(R.layout.presentation_layout); // 获取布局中的 TextView,并设置显示文本 mText = findViewById(R.id.presentation_text); mText.setText("This is my presentation!"); } } public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取 DisplayManager 实例 DisplayManager displayManager = (DisplayManager) getSystemService(Context.DISPLAY_SERVICE); // 获取所有的外部显示设备 Display[] displays = displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION); // 如果有外部显示设备,则在第一个设备上显示内容 if (displays.length > 0) { MyPresentation presentation = new MyPresentation(this, displays[0]); presentation.show(); } else { Log.e(TAG, "No external displays found!"); } } }
这是一个 Android 应用程序的代码,它展示了如何在外部显示设备上显示内容。应用程序中定义了一个 MyPresentation 类,它继承自 Presentation 类,用于显示在外部显示设备上的内容。在 MainActivity 中,获取了 DisplayManager 实例并获取了所有的外部显示设备,如果有外部显示设备,则在第一个设备上显示 MyPresentation 的内容。如果没有外部显示设备,则输出错误日志。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)