サービスを開始する Android アクティビティを作成しています。このサービスは、ユーザーが他のアプリケーションを使用している場合でも、タッチ イベントを受信することを目的としています。その onCreate() メソッドは次のとおりです。public void 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);
}
上記では、画面の高さと幅全体にまたがるウィンドウを作成しています。タッチイベントをリッスンするように設定しました。ただし、そうすると、他のアプリケーションがタッチ イベントを受信しなくなります。したがって、サービスで受信したこれらのタッチイベントを、ウィンドウが配置されているバックグラウンドアプリケーションにディスパッチしようとしています。
助けてください !