我尝试搜索,但没有直接和/或清晰的答案。
开发者网站的定义也不明确:
应用程序用来与窗口管理器交互的接口。 使用
Context.getSystemService(Context.WINDOW_SERVICE)
获取其中之一。
有能力用通俗易懂的英语解释一下是什么吗?
如何使用它创建一个浮动对象,使其在多个活动之间保持不变,即使我从一个活动到另一个活动?
我尝试搜索,但没有直接和/或清晰的答案。
开发者网站的定义也不明确:
应用程序用来与窗口管理器交互的接口。 使用
Context.getSystemService(Context.WINDOW_SERVICE)
获取其中之一。
有能力用通俗易懂的英语解释一下是什么吗?
如何使用它创建一个浮动对象,使其在多个活动之间保持不变,即使我从一个活动到另一个活动?
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);
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
针对 Android API 版本大于23的情况,需要在运行时请求 android.permission.SYSTEM_ALERT_WINDOW
权限。此外,在 Android API 26 中,TYPE_SYSTEM_ERROR
和其他少数类型已经被弃用。以下是解决方法:
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;
}
窗口管理器组织屏幕并处理应该放置在哪里以及如何分层。
以下是一个漂浮对象的优秀开源示例。 漂浮对象示例
windowManager.removeView()
时,会在移除视图期间出现一些闪烁。如何避免这种情况? - Chaitanya
TYPE_APPLICATION_OVERLAY
标志的那个? - Elye