android - 什么是android中的WindowManager?

标签 android android-windowmanager

我尝试用谷歌搜索,但没有直接和/或明确的答案。

developer website的定义也不清楚:

The interface that apps use to talk to the window manager. Use Context.getSystemService(Context.WINDOW_SERVICE) to get one of these.

6 年级英语简单的人能解释一下它是什么吗?

我如何使用它来创建一个 float 对象,即使我从一个移动到另一个,也可以通过多个 activties 保留?

最佳答案

Android WindowManager 是一个系统服务,它负责管理窗口的 z 顺序列表、哪些窗口是可见的以及它们在屏幕上的布局方式。除其他外,它会在打开或关闭应用程序或旋转屏幕时自动执行窗口转换和动画。

每个 Activity 都有一个窗口,用于在屏幕上显示其内容。当您在 Activity 上调用 setContentView 时,它会将该 View 附加到 Activity 的默认窗口。默认窗口会填满屏幕,因此您的 Activity 窗口会隐藏任何其他 Activity ——WindowManager 将显示位于顶部的任何窗口。所以通常你不需要担心 windows - 你只需创建一个 Activity ,Android 会为你做剩下的事情。

但是如果你想做一些不寻常的事情,比如创建不填满屏幕的 float 窗口,你需要与 WindowManager 交互。如果要创建一个在其他应用程序面前可见的 float 窗口,则不能使用 Activity,因为当另一个应用程序进入前台时,您的 Activity 将停止,并且其窗口将被隐藏或销毁。相反,您需要显示来自后台服务的窗口。例如:

WindowManager.LayoutParams p = new WindowManager.LayoutParams(
    // Shrink the window to wrap the content rather than filling the screen 
    WindowManager.LayoutParams.WRAP_CONTENT,
    WindowManager.LayoutParams.WRAP_CONTENT,
    // Display it on top of other application windows, but only for the current user
    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
    // Don't let it grab the input focus
    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
    // Make the underlying application window visible through any transparent parts
    PixelFormat.TRANSLUCENT);

// Define the position of the window within the screen
p.gravity = Gravity.TOP | Gravity.RIGHT;
p.x = 0;
p.y = 100;

WindowManager windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
windowManager.addView(myView, p);

为此,您需要向您的 AndroidManifest.xml 添加以下权限

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

关于android - 什么是android中的WindowManager?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19846541/

相关文章:

android - getAuthToken y Android 未调用任何回调

android - PhoneGap 对广告的支持

Android:如何安排?

Android webview 适合所有内容到屏幕

android - 接听电话或电话停止响铃时关闭 Android 上的自定义来电屏幕

android - 为什么只在 Nougat 的 Activity 转换中删除添加到 WindowManager 的 View ?

java - 如何在我的 java 文件中实现旋转器?

android - 我可以模拟 NFC-V 标签吗?

android - 在 Android 4.2.2 中将 screenBrightness 属性设置为 0.0f 不再关闭屏幕?

android - 无法删除叠加 View