2013-08-18 28 views
0

我正在創建啓動服務的android活動。即使用戶正在使用其他應用程序,此服務也用於接收觸摸事件。其onCreate()方法如下。 公共無效的onCreate(){將觸摸事件分派到其他應用程序

super.onCreate(); 
    // create linear layout 
    touchLayout = new LinearLayout(this); 
    // set layout width 30 px and height is equal to full screen 
    LayoutParams lp = new LayoutParams(30, LayoutParams.MATCH_PARENT); 
    touchLayout.setLayoutParams(lp); 
    // set color if you want layout visible on screen 
    //touchLayout.setBackgroundColor(Color.CYAN); 
    // set on touch listener 
    touchLayout.setOnTouchListener(this); 

    // fetch window manager object 
    mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE); 
    // set layout parameter of window manager 
    WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(
       //30, // width of layout 30 px 
      WindowManager.LayoutParams.MATCH_PARENT, 
       WindowManager.LayoutParams.MATCH_PARENT, // height is equal to full screen 
       WindowManager.LayoutParams.TYPE_PHONE, // Type Ohone, These are non-application windows providing user interaction with the phone (in particular incoming calls). 
       WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE , // this window won't ever get key input focus 
       PixelFormat.TRANSLUCENT);  
    mParams.gravity = Gravity.LEFT | Gravity.TOP; 
    Log.i(TAG, "add View"); 

    mWindowManager.addView(touchLayout, mParams); 

} 

上面,我是創造跨越全高和屏幕寬度的窗口。我已將其設置爲傾聽觸摸事件。但這樣做會阻止其他應用程序收到觸摸事件。所以我正在尋找將我的服務上收到的這些觸摸事件發送到放置我的窗口的後臺應用程序。

請幫忙!

回答

0

的Sandip,

添加一個覆蓋視圖窗口管理器會自動把這個觀點在那個窗口的視圖層次結構的頂部,這意味着它會攔截,而不是視圖(S)背後的一面。觸摸事件背後的唯一方式是讓用戶觸摸頂視圖之外(這是不可能的,因爲它橫跨整個屏幕),或者頂視圖將自身標記爲「不可觸摸」。

所以,無論您的視圖限制的大小,或標誌WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE添加到您的PARAMS標誌。

無論哪種情況,除非用戶目前在前臺有您的應用程序,否則視圖外的任何觸摸事件都將返回ACTION_OUTSIDE觸摸事件,但在觸摸位置上沒有任何座標。 (如果用戶確實在前臺使用了您的應用,那麼您將收到ACTION_OUTSIDE事件的座標。)

相關問題