Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

无法同时创建多个系统悬浮窗 #6

Closed
Gennki opened this issue Aug 22, 2019 · 4 comments
Closed

无法同时创建多个系统悬浮窗 #6

Gennki opened this issue Aug 22, 2019 · 4 comments

Comments

@Gennki
Copy link

Gennki commented Aug 22, 2019

连续两次创建悬浮窗代码如下

    override fun openFloatWindow() {
        EasyFloat.with(AppManager.getAppManager().currentActivity())
            // 设置浮窗xml布局文件
            .setLayout(R.layout.float_window_select_question)
            // 设置浮窗显示类型,默认只在当前Activity显示,可选一直显示、仅前台显示
            .setShowPattern(ShowPattern.ALL_TIME)
            // 设置浮窗的标签,用于区分多个浮窗
            .setTag("selectQuestion")
            // 设置浮窗固定坐标,ps:设置固定坐标,Gravity属性和offset属性将无效
            .setLocation(ConvertUtils.dp2px(64f), ConvertUtils.dp2px(128f))
            // 设置我们传入xml布局的详细信息
            .invokeView(OnInvokeView {
                val rootView = it.findViewById<ConstraintLayout>(R.id.cl_root_view)
                val params = rootView.layoutParams as ViewGroup.LayoutParams
                it.findViewById<ScaleImage>(R.id.img_scale).onScaledListener =
                    object : ScaleImage.OnScaledListener {
                        override fun onScaled(x: Float, y: Float, event: MotionEvent) {
                            params.width += x.toInt()
                            params.height += y.toInt()
                            rootView.layoutParams = params
                        }
                    }
            })
            .show()

        EasyFloat.with(AppManager.getAppManager().currentActivity())
            // 设置浮窗xml布局文件
            .setLayout(R.layout.float_window_get_answer)
            // 设置浮窗显示类型,默认只在当前Activity显示,可选一直显示、仅前台显示
            .setShowPattern(ShowPattern.ALL_TIME)
            // 设置浮窗的标签,用于区分多个浮窗
            .setTag("getAnswer")
            // 设置浮窗固定坐标,ps:设置固定坐标,Gravity属性和offset属性将无效
            .setLocation(ConvertUtils.dp2px(30f), ConvertUtils.dp2px(500f))
            // 设置我们传入xml布局的详细信息
            .invokeView(OnInvokeView {

            })
            // 创建浮窗(不要忘记哦😂)
            .show()
    }

排查了下,发现原因是因为FloatService中的config属性导致

companion object {
        private const val FLOAT_ACTION = "floatAction"
        private const val FLOAT_VISIBLE = "floatVisible"
        private const val FLOAT_DISMISS = "floatDismiss"
        private const val FLOAT_TAG = "floatTag"
        const val DEFAULT_TAG = "default"
        val floatMap = mutableMapOf<String, AppFloatManager>()
        private var config = FloatConfig()

        /**
         * 开启创建浮窗的Service
         */
        fun startService(context: Context, floatConfig: FloatConfig) {
            config = floatConfig
            context.startService(Intent(context, FloatService::class.java))
        }
}

由于config是静态变量,因此当连续两次调用startService的时候,第二次的config中的tag会把第一次config中的tag给覆盖掉,导致第二次的checkTag()方法返回false

另外这是个很棒的库,感谢大佬,最近项目正好需要用到

@Gennki
Copy link
Author

Gennki commented Aug 22, 2019

补充,目前我在两次创建悬浮窗之间临时加了一秒延时,可以解决这个问题

@princekin-f
Copy link
Owner

你好,同时创建多个系统浮窗,确实会存在该问题。

  • 可以在startService的时候,通过Intent传递config,这样可以确保config的不同;
  • 但是部分数据需要自行实现序列化,而且序列化、反序列化都是耗时的;
  • 针对你这种需求,还是建议设置延时创建,或者在OnFloatCallbacks拿到回调结果再创建。

@Gennki Gennki closed this as completed Aug 28, 2019
@DamonChen117
Copy link

我试了下延时也不能显示2个。
这个问题可以reopen,正式解决下吗?

@MachineHou
Copy link

我的解决方案
1、 .setTag(tag)---- 注(tag是通过随机数标注,判断每次的tag如果重复就使用 EasyFloat.dismiss(tag, true);关闭掉前一个EasyFloat),这样就不会出现重复的了也不会包,或者你想重复创建多个系统悬浮窗页可以使用此方法做一个延迟处理就可以,-----我这一步主要解决的是关掉多个tag的问题

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants