Android 应用程序:当应用程序处于后台操作时,如何在主屏幕上获取触摸事件

问题描述

使用 WindowManager,我像 Facebook Messenger 一样在主屏幕上制作应用程序 floaintView。

MotionEvent 允许移动浮动视图。 我想设置 WindowManager Flag 并为 Floating 以外的屏幕获取触摸事件,但它不起作用...

当我触摸浮动视图时,它运行良好。通过调用 OnTouchListener,ACTION_DOWN ACTION_UP,

当我在窗口(主屏幕)外触摸时,我希望它在做其他工作(触摸其他应用程序)和调用侦听器时都能很好地工作 请任何人指导我如何在我的应用程序中获取触摸事件,以便我可以完成一些任务。 我使用了三个 all 标志 FLAG_WATCH_OUTSIDE_TOUCH FLAG_NOT_TOUCH_MODAL
FLAG_NOT_FOCUSABLE

  1. FLAG_NOT_FOCUSABLE |FLAG_NOT_TOUCH_MODAL
    在主屏幕上,它会触摸窗口的其余部分,但无法调用 OnClickListener 的触摸。

  2. FLAG_WATCH_OUTSIDE_TOUCH 浮动视图以外的触摸被阻止。

    @SuppressLint("ClickableViewAccessibility") 覆盖乐趣 onCreate() { super.onCreate() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) startMyOwnForeground() else startForeground( 1、 通知() )

     //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
             }
         })
     }
    

    }

'''

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)