Android 使用 WindowManager 打造悬浮通知

最近项目中遇到使用通知栏的功能,开始使用Notification的时候,发现存在兼容问题,于是换成了
WindowManager , 发现 WindowManager 能够很好的解决问题。
先看下效果图

微信图片_20171212150512.png

以下是详细内容 ps:代码用的Kotlin

首先布局文件



    

        

        

            

            

        

    


其次 在Application中创建 WindowManager.LayoutParams

class App : Application() {

    companion object {
        private var wmParams: WindowManager.LayoutParams? = null

        fun getWindowLayoutParams(): WindowManager.LayoutParams? = wmParams
    }

    override fun onCreate() {
        super.onCreate()
        wmParams = WindowManager.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_PRIORITY_PHONE,
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS or WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, PixelFormat.TRANSLUCENT)
    }

   ...

}

然后在Activity中使用

class MainActivity : BaseActivity() {

    private var mWindowManager: WindowManager? = null
    private var mWindowParams: WindowManager.LayoutParams? = null
    private var floatView: View? = null

    override fun getLayoutId(): Int = R.layout.activity_main

    override fun initView() {
    }

    override fun onViewClick() {
        super.onViewClick()
        RxView.clicks(mainNotifyBtn)
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribeBy(onNext = {
                    showWindowAlert()
                }, onError = {

                })
        RxView.clicks(mainCancelNotifyBtn)
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribeBy(onNext = {
                        hideWindowAlert()
                }, onError = {

                })
    }

    override fun isSupportBack(): Boolean = false

    override fun getToolBarTitle(): String? = "San"

    override fun processLogic() {

    }

    private fun showWindowAlert() {
        mWindowManager = applicationContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        mWindowParams = App.getWindowLayoutParams()
        floatView = layoutInflater.inflate(R.layout.ic_window_float, null)

//        mWindowParams!!.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
//        mWindowParams!!.format = 1
//        mWindowParams!!.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE // 不能抢占聚焦点
//        mWindowParams!!.flags = mWindowParams!!.flags or WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
//        mWindowParams!!.flags = mWindowParams!!.flags or WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS // 排版不受限制
//        mWindowParams!!.flags = mWindowParams!!.flags or WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS //
//        mWindowParams!!.alpha = 0F
        mWindowParams!!.gravity = Gravity.TOP

        mWindowParams!!.x = 0
        mWindowParams!!.y = 0

        mWindowParams!!.verticalMargin = 0F
        mWindowParams!!.horizontalMargin = 15F

//        mWindowParams!!.width = floatView!!.layoutParams.width
//        mWindowParams!!.height = floatView!!.layoutParams.height

        mWindowManager!!.addView(floatView, mWindowParams)
    }

    private fun hideWindowAlert() {
        if (mWindowManager != null)
            mWindowManager!!.removeViewImmediate(floatView)
    }

    override fun onDestroy() {
        super.onDestroy()
        hideWindowAlert()
    }
}

可以根据实际功能需求,给FloatView添加动画
按照上面的代码做,功能就能实现了。谢谢!

你可能感兴趣的:(Android 使用 WindowManager 打造悬浮通知)