Android跟踪所有手势/触摸甚至是在后台运行的应用程序

时间:2013-11-20 15:44:11

标签: java android overlay ontouchlistener window-managers

我写了这段代码并且工作正常,

用于测试它会创建一个叠加视图并听取触摸,到目前为止一切都很好, 但是怎么能把这个带到后台,仍然听听触摸听众。

我的想法是,我想跟踪用户用手机做的所有手势......

    public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View startView = inflater.inflate(R.layout.activity_main, null);
        setContentView(startView);

        changeToInvisible();
        startView.setVisibility(View.GONE);
    }


     private void changeToInvisible(){

           LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           View inviView = inflater.inflate(R.layout.invisble,null);

            WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                    PixelFormat.TRANSLUCENT);

            WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
            wm.addView(inviView, params);

           inviView.setOnTouchListener( new OnTouchListener() {
                @Override
                public boolean onTouch(View inviView, MotionEvent event) {
                   Log.d("tag",  "touch from invisible app");
                return true;
                }
            });    
    }

}

invisble.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/trans"
    android:orientation="vertical" >

</LinearLayout>

color trans:

 <color name="trans">#80000000</color>

0 个答案:

没有答案