1

Using WindowManager, I make the app floaintView in home screen like Facebook Messenger.

MotionEvent allows to move the Floated View. I'd like to set up WindowManager Flag and get a touch event for a screen other than Floating, but it's not working...

When I touch the floating view, It works well. By calling OnTouchListener, ACTION_DOWN ACTION_UP,

When I touch outside the Window( Home Screen), I Want it works well both Doing other Job(Touching other App) and Calling Listener Please anyone guide me how I can get touch event in my app so I can do some task. I used three all flags FLAG_WATCH_OUTSIDE_TOUCH FLAG_NOT_TOUCH_MODAL
FLAG_NOT_FOCUSABLE

  1. FLAG_NOT_FOCUSABLE |FLAG_NOT_TOUCH_MODAL
    On the Home screen, it touchs of the rest of the window, but fails to invoke the touch of the OnClickListener.

  2. FLAG_WATCH_OUTSIDE_TOUCH Touch other than Floating View is blocked.

    @SuppressLint("ClickableViewAccessibility") override fun onCreate() { super.onCreate() if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) startMyOwnForeground() else startForeground( 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
             }
         })
     }
    

    }

'''

정모세
  • 11
  • 1

0 Answers0