android 悬浮窗

以重绘状态栏,遮盖状态栏为例:

1、设置 WindowManager.LayoutParams 属性

  public static WindowManager.LayoutParams getStatusViewParams(Context ctx, WindowManager mWindowManager) {
        //反射状态栏高度
        int actionBarHight = 0;
        int resourceId = ctx.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            actionBarHight = ctx.getResources().getDimensionPixelSize(resourceId);
        }
        // 设置LayoutParams(全局变量)相关参数
        WindowManager.LayoutParams mStatusBarParams = new WindowManager.LayoutParams();
        //TYPE_SYSTEM_ERROR 需要控件获取焦点时才需要  TYPE_SYSTEM_OVERLAY
        mStatusBarParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
        mStatusBarParams.format = PixelFormat.RGBA_8888;
        mStatusBarParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_FULLSCREEN
                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
        mStatusBarParams.gravity = Gravity.CENTER | Gravity.TOP; // 调整悬浮窗口至左上角
        // 以屏幕左上角为原点,设置x、y初始值
        mStatusBarParams.x = 0;
        mStatusBarParams.y = 0;
        // 设置悬浮窗口长宽数据
        mStatusBarParams.width = mWindowManager.getDefaultDisplay().getWidth();
        mStatusBarParams.height = actionBarHight;
        //设置加载动画
        mStatusBarParams.windowAnimations=android.R.style.Animation_Translucent;
        Log.e(TAG,"width:"+mStatusBarParams.width +"\nheight:" + mStatusBarParams.height );
        return mStatusBarParams;
    }

需要重点注意 :WindowManager.LayoutParams的type属性

TYPE_SYSTEM_ERROR 需要控件获取焦点时才需要(例如下拉、点击事件等);
TYPE_SYSTEM_OVERLAY 不可以获取焦点; 
TYPE_TOAST  不需要权限显示悬浮窗;从4.4开始, 使用TYPE_TOAST的同时还可以接收触摸事件和按键事件了, 而4.4以前只能显示出来,            不能交互.

2、添加、修改、移除悬浮窗方法

addView、updateViewLayout、removeView

3、附注

1)悬浮窗添加流程:

WindowManager.addView -> ViewRootImpl.setView -> WindowSession.addToDisplay(AIDL进行IPC) -> WindowManagerService.addWindow() -> ViewRootImpl.setView

2)反射通知栏操作:

 public static void OpenNotify(Context ctx) {
        // TODO Auto-generated method stub
        int currentApiVersion = android.os.Build.VERSION.SDK_INT;
        try {
            Object service = ctx.getSystemService("statusbar");
            Class<?> statusbarManager = Class
                    .forName("android.app.StatusBarManager");
            Method expand = null;
            if (service != null) {
                if (currentApiVersion <= 16) {
                    expand = statusbarManager.getMethod("expand");
                } else {
                    expand = statusbarManager
                            .getMethod("expandNotificationsPanel");
                }
                expand.setAccessible(true);
                expand.invoke(service);
            }
        } catch (Exception e) {
        }
    }

 

 

 
 

 

  

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值