android应用程序:当应用程序在后台运行时,如何在主屏幕上获取触摸事件

42fyovps  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(335)

使用windowmanager,我使应用程序在主屏幕上像facebook messenger一样显示。
motionevent允许移动浮动视图。我想设置windowmanager标志,并为浮动屏幕以外的屏幕获取触摸事件,但它不起作用。。。
当我触摸浮动视图时,它工作得很好。通过调用ontouchlistener,action\u down action\u up,
当我触摸窗外(主屏幕)时,我希望它能很好地完成其他工作(触摸其他应用程序)和呼叫侦听器。请任何人指导我如何在我的应用程序中获取触摸事件,以便我可以执行一些任务。我用了三个全旗旗、旗帜、手表、户外、触摸旗、非触摸模式
标志不可聚焦
flag_not_focusable| flag_not_touch_model
在主屏幕上,它触摸窗口的其余部分,但无法调用onclicklistener的触摸。
除浮动视图外的标志\u手表\u外部触摸触摸被阻止。
@suppresslint(“clickableviewaccessibility”)覆盖fun oncreate(){super.oncreate(),如果(build.version.sdk\u int>=build.version\u codes.o)startmyownforeground(),否则启动Foreground(1,notification())

//Inflate the floating view layout we created
 mFloatingView = LayoutInflater.from(this).inflate(R.layout.test, null)

 val LAYOUT_FLAG: Int
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
     LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY

 } else {
     LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE

 }

 params = WindowManager.LayoutParams(
     WindowManager.LayoutParams.WRAP_CONTENT,
     WindowManager.LayoutParams.WRAP_CONTENT,
     LAYOUT_FLAG,
     WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or  WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
     PixelFormat.TRANSLUCENT
 )

 //Specify the view position
 params!!.gravity =
     Gravity.TOP or Gravity.LEFT //Initially view will be added to top-left corner
 params!!.x = 0
 params!!.y = 100

 //Add the view to the window
 mWindowManager = getSystemService(WINDOW_SERVICE) as WindowManager
 mWindowManager!!.addView(mFloatingView, params)

 var test = mFloatingView!!.findViewById<RelativeLayout>(R.id.wrap_container)
 with(test) {
     setOnTouchListener(object : View.OnTouchListener {
         private var initialX = 0
         private var initialY = 0
         private var initialTouchX = 0f
         private var initialTouchY = 0f
         override fun onTouch(v: View?, event: MotionEvent): Boolean {
             Log.d("aa", "aa")
             when (event.action) {
                 MotionEvent.ACTION_DOWN -> {
                     Log.d("ACTION_DOWN", "ACTION_DOWN")
                     //remember the initial position.
                     initialX = params!!.x
                     initialY = params!!.y

                     //get the touch location
                     initialTouchX = event.rawX
                     initialTouchY = event.rawY

                     return true
                 }
                 MotionEvent.ACTION_UP -> {
                     Log.d("ACTION_UP", "ACTION_UP")

                     val Xdiff = (event.rawX - initialTouchX).toInt()
                     val Ydiff = (event.rawY - initialTouchY).toInt()

                     //The check for Xdiff <10 && YDiff< 10 because sometime elements moves a little while clicking.
                     //So that is click event.
                     if (Xdiff < 10 && Ydiff < 10) {
                         val intent = Intent(applicationContext, MainActivity::class.java)
                         intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                         intent.putExtra("fromwhere", "ser")
                         startActivity(intent)
                     }
                     return true
                 }
                 MotionEvent.ACTION_MOVE -> {

                     Log.d("ACTION_MOVE", "ACTION_MOVE")
                     //Calculate the X and Y coordinates of the view.
                     params!!.x = initialX + (event.rawX - initialTouchX).toInt()
                     params!!.y = initialY + (event.rawY - initialTouchY).toInt()

                     //Update the layout with new X & Y coordinate
                     mWindowManager!!.updateViewLayout(mFloatingView, params)
                     return true
                 }
                 MotionEvent.ACTION_OUTSIDE -> {

                     Log.d("ACTION_OUTSIDE", "ACTION_OUTSIDE")
                     //Calculate the X and Y coordinates of the view.
                     initialX = params!!.x
                     initialY = params!!.y

                     //get the touch location
                     initialTouchX = event.rawX
                     initialTouchY = event.rawY

                     return true
                 }
             }
             return false
         }
     })
 }

}
'''

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题