我试过谷歌上搜索它并没有直接和/或清除切的答案。

开发网站's的定义是不明确的是:

该接口的应用程序用来帮你的窗口的管理。使用 Context.getSystemService(Context.WINDOW_SERVICE) 得到这些。

有人可以与普通的6年级的英语解释一下它是什么?

我怎么能用它来创建一个浮动的对象仍然是通过若干活动,尽管我从一个到另一个?

有帮助吗?

解决方案

Android WindowManager是一项系统服务,负责管理Windows的Z顺序列表,Windows是可见的,以及如何在屏幕上布置它们。除其他外,它在打开或关闭应用程序或旋转屏幕时会自动执行窗口过渡和动画。

每个活动都有一个用于在屏幕上显示其内容的窗口。当您在活动上调用SetContentView时,它将该视图附加到活动的默认窗口。默认窗口填充了屏幕,因此您的活动的窗口隐藏了所有其他活动 - WindowManager将显示最顶部的窗口。因此,通常您不必担心Windows - 您只需创建一个活动,Android将为您服务。

但是,如果您想做一些不寻常的事情,例如创建不填充屏幕的浮动窗口,则需要与WindowManager进行交互。如果要创建一个在其他应用程序面前可见的浮动窗口,则无法使用活动,因为当另一个应用程序进入前景时,您的活动将停止,并且其窗口将被隐藏或破坏。相反,您需要显示背景服务的窗口。例如:

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 API版本> 23, android.permission.SYSTEM_ALERT_WINDOW 需要请求运行时。而且, TYPE_SYSTEM_ERROR 在Android API 26中,有几种类型被弃用。

public void showWindowManager() {
    if (requestPermission()) {
        return;
    }

    WindowManager.LayoutParams p =
            new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    Build.VERSION.SDK_INT > Build.VERSION_CODES.O
                            ? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
                            : WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                    PixelFormat.TRANSLUCENT);


    final WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    LayoutInflater layoutInflater =
            (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    final View popupView = layoutInflater.inflate(R.layout.window_manager_layout, null);
    windowManager.addView(popupView, p);

    // dismiss windowManager after 3s
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            windowManager.removeView(popupView);
        }
    }, 3000);
}

@TargetApi(Build.VERSION_CODES.M)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE) {
        if (Settings.canDrawOverlays(this)) {
            showWindowManager();
        }
    }
}

public boolean requestPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.canDrawOverlays(this)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                    Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, ACTION_MANAGE_OVERLAY_PERMISSION_REQUEST_CODE);
            return true;
        }
    }
    return false;
}

窗口管理组织的屏幕和进程应该怎么去那里和他们应该如何分层。

这里是一个很好的开放源码的例子的一个浮动的对象。浮动对象的例子

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top