什么是Android中的WindowManager?

我尝试使用Googlesearch,没有直接和/或清晰的答案。

开发者网站的定义还不清楚:

应用程序用来与窗口pipe理器交谈的界面。 使用Context.getSystemService(Context.WINDOW_SERVICE)来获取其中的一个。

可以用简单的六年级英语解释这是什么?

我怎样才能使用它创build一个浮动的对象,通过几个活动保持,即使我从一个移动到另一个?

Android WindowManager是一个系统服务,它负责pipe理窗口的z顺序列表,窗口是可见的,以及它们在屏幕上的布局。 除此之外,它在打开或closures应用程序或旋转屏幕时自动执行窗口转换和animation。

每个活动都有一个窗口,用于在屏幕上显示其内容。 当您在活动上调用setContentView时,它会将该视图附加到活动的默认窗口。 默认的窗口填满了屏幕,所以你的活动的窗口隐藏了其他任何活动–WindowManager将显示最上面的窗口。 所以通常你不需要担心窗口 – 你只需要创build一个活动,Android会为你完成剩下的工作。

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

 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; px = 0; py = 100; WindowManager windowManager = (WindowManager)getSystemService(WINDOW_SERVICE); windowManager.addView(myView, p); 

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

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

窗口pipe理器组织屏幕并处理应该在哪里以及如何分层的过程。

这是一个漂亮的开源的浮动对象的例子。 浮动对象示例

对于android api版本> 23, android.permission.SYSTEM_ALERT_WINDOW需要请求运行时。 而且, TYPE_SYSTEM_ERROR和一些types在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; }