WindowManager 源码分 TYPE_TOAST 悬浮窗权限检探究

本文分析了WindowManager接口及其实现类,重点关注了如何通过Context获取WindowManager以及WindowManagerImpl的实现。在WindowManagerGlobal中,添加View的权限检查涉及PolicyManager,特别是对于TYPE_TOAST类型的悬浮窗,4.4_r1之前没有做权限检查,而在4.4之后,TYPE_TOAST可以接收触摸事件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WindowManager类是一个接口类,继承制ViewManager,Viewmanager是一个简单的接口类,很简单的理解可以知道是对View进行管理的类,里面只有三个方法,下面是源码:

 

源码分析:WindowManager,TYPE_TOAST悬浮窗权限检测问题


WindowManager类是一个接口类,继承制ViewManager,Viewmanager是一个简单的接口类,很简单的理解可以知道是对View进行管理的类,里面只有三个方法,下面是源码:
package android.view;

/** Interface to let you add and remove child views to an Activity. To get an instance
  * of this class, call {@link android.content.Context#getSystemService(java.lang.String) Context.getSystemService()}.
  */
public interface ViewManager
{
    /**
     * Assign the passed LayoutParams to the passed View and add the view to the window.
     * <p>Throws {@link android.view.WindowManager.BadTokenException} for certain programming
     * errors, such as adding a second view to a window without removing the first view.
     * <p>Throws {@link android.view.WindowManager.InvalidDisplayException} if the window is on a
     * secondary {@link Display} and the specified display can't be found
     * (see {@link android.app.Presentation}).
     * @param view The view to be added to this window.
     * @param params The LayoutParams to assign to view.
     */
    public void addView(View view, ViewGroup.LayoutParams params);
    public void updateViewLayout(View view, ViewGroup.LayoutParams params);
    public void removeView(View view);
}


下面是WindowMananger的源码头部:

/**
 * The interface that apps use to talk to the window manager.
 * <p>
 * Use <code>Context.getSystemService(Context.WINDOW_SERVICE)</code> to get one of these.
 * </p><p>
 * Each window manager instance is bound to a particular {@link Display}.
 * To obtain a {@link WindowManager} for a different display, use
 * {@link Context#createDisplayContext} to obtain a {@link Context} for that
 * display, then use <code>Context.getSystemService(Context.WINDOW_SERVICE)</code>
 * to get the WindowManager.
 * </p><p>
 * The simplest way to show a window on another display is to create a
 * {@link Presentation}.  The presentation will automatically obtain a
 * {@link WindowManager} and {@link Context} for that display.
 * </p>
 *
 * @see android.content.Context#getSystemService
 * @see android.content.Context#WINDOW_SERVICE
 */
public interface WindowManager extends ViewManager 
我们从@see 可以看到我们获取WindowManager的方法是Context的getSystemService方法,只需要传入参数WINDOW_SERVICE就行,比如下面

WindowManager mWm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);


WindowManager的实现类是WindowManagerImpl,见下面源码,
这个WindowManagerIpml有两个,分别位于不同的包下面,
package android.view;
package com.android.layoutlib.bridge.android.view;

第一个:WindowManagerImpl
package android.view;

package android.view;

import android.annotation.NonNull;
import android.os.IBinder;

/**
 * Provides low-level communication with the system window manager for
 * operations that are bound to a particular context, display or parent window.
 * Instances of this object are sensitive to the compatibility info associated
 * with the running application.
 *
 * This object implements the {@link ViewManager} interface,
 * allowing you to add any View subclass as a top-level window on the screen.
 * Additional window manager specific layout parameters are defined for
 * control over how windows are displayed.  It also implements the {@link WindowManager}
 * interface, allowing you to control the displays attached to the device.
 * 
 * <p>Applications will not normally use WindowManager directly, instead relying
 * on the higher-level facilities in {@link android.app.Activity} and
 * {@link android.app.Dialog}.
 * 
 * <p>Even for low-level window manager access, it is almost never correct to use
 * this class.  For example, {@link android.app.Activity#getWindowManager}
 * provides a window manager for adding windows that are associated with that
 * activity -- the window manager will not normally allow you to add arbitrary
 * windows that are not associated with an activity.
 *
 * @see WindowManager
 * @see WindowManagerGlobal
 * @hide
 */
public final class WindowManagerImpl implements WindowManager {
    private final WindowManagerGlobal mGlobal = WindowManagerGlobal.getInstance();
    private final Display mDisplay;
    private final Window mParentWindow;

    private IBinder mDefaultToken;<

对于 Android 悬浮窗的实现,可以使用 WindowManager 来实现。但是在实现过程中,可能会遇到 WindowManager 无法传递事件的问题。 这个问题的原因可能是因为 WindowManager 的类型不正确。在创建 WindowManager 的时候,需要指定正确的类型,例如 TYPE_APPLICATION_OVERLAY 类型。 下面是一个基本的悬浮窗实现示例: ```java public class FloatingWindowService extends Service { private WindowManager mWindowManager; private View mFloatingView; public FloatingWindowService() { } @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); // 创建一个 WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE); // 创建一个悬浮窗 View mFloatingView = LayoutInflater.from(this).inflate(R.layout.floating_window, null); // 设置悬浮窗的类型为 TYPE_APPLICATION_OVERLAY WindowManager.LayoutParams params = 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_PHONE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); // 将悬浮窗添加到 WindowManager 中 mWindowManager.addView(mFloatingView, params); } @Override public void onDestroy() { super.onDestroy(); if (mFloatingView != null) { // 从 WindowManager 中移除悬浮窗 mWindowManager.removeView(mFloatingView); } } } ``` 在这个示例中,我们通过创建一个 WindowManager,并将悬浮窗 View 添加到 WindowManager 中来实现悬浮窗。同时,我们还需要设置悬浮窗的类型为 TYPE_APPLICATION_OVERLAY 类型,以确保能够正常接收事件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值