What is WindowManager in android?

I tried looking for it, and there is no direct and / or clear answer.

Developer site definition:

The interface that applications use to work with the window manager. using Context.getSystemService(Context.WINDOW_SERVICE) to get one of them.

Can anyone with plain 6th grade language explain what it is?

And how can I use it to create a floating object that stays through several functions, although I moved from one to the other?

+45
android android-windowmanager
Nov 07 '13 at
source share
3 answers

Android WindowManager is a system service that is responsible for managing a z-ordered list of windows, which windows are visible and how they are laid out on the screen. Among other things, it automatically performs window transitions and animations when you open or close an application or when you rotate the screen.

Each action has a window that is used to display its contents on the screen. When you call setContentView in action, it attaches this view to the default activity window. The window fills the screen by default, so your activity window hides any other actions - WindowManager will display any window on top. Therefore, as a rule, you do not need to worry about Windows - you just create activity, and Android will do everything for you.

But you need to interact with WindowManager if you want to do something unusual, like creating floating windows that don't fill the screen. If you want to create a floating window that is visible in front of other applications, you cannot use the action because your activity will stop when another application appears in the foreground and its window is hidden or destroyed. Instead, you need to display a window from the background service. For example:

 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); 

To do this, you will need to add the following permissions to your AndroidManifest.xml

 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/> 
+95
Mar 16 '14 at 20:40
source share

The window manager organizes the screen and processes what should go where and how they should be layered.

Here is a good open source example for a floating object. Floating example

+3
Nov 07 '13 at 21:34
source share

For android version api> 23, android.permission.SYSTEM_ALERT_WINDOW you need to request a runtime. Moreover, TYPE_SYSTEM_ERROR and several types are deprecated in android api 26. Here is the way

 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; } 
0
Oct 20 '17 at 10:21 on
source share



All Articles